
<span id="indexing"></span><h1><span class="yiyi-st" id="yiyi-83">Indexing and Selecting Data</span></h1>
        <blockquote>
        <p>原文：<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html">http://pandas.pydata.org/pandas-docs/stable/indexing.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<p><span class="yiyi-st" id="yiyi-84">pandas对象中的轴标签信息有很多用途：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-85">使用已知指标识别数据（即提供<em>元数据</em>），对于分析，可视化和交互式控制台显示非常重要</span></li>
<li><span class="yiyi-st" id="yiyi-86">启用自动和显式数据对齐</span></li>
<li><span class="yiyi-st" id="yiyi-87">允许直观获取和设置数据集的子集</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-88">在本节中，我们将关注最后一点：即，如何切片，删除，以及一般获取和设置pandas对象的子集。</span><span class="yiyi-st" id="yiyi-89">主要关注的是系列和数据框架，因为他们在这一领域受到更多的发展关注。</span><span class="yiyi-st" id="yiyi-90">期望在将来对更高维数据结构（包括<code class="docutils literal"><span class="pre">Panel</span></code>）投入更多的工作，尤其是在基于标签的高级索引中。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-91">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-92">Python和NumPy索引操作符<code class="docutils literal"><span class="pre">[]</span></code>和属性操作符<code class="docutils literal"><span class="pre">.</span></code></span><span class="yiyi-st" id="yiyi-93">提供在大范围的用例中快速和容易地访问pandas数据结构。</span><span class="yiyi-st" id="yiyi-94">这使得交互式工作直观，因为如果你已经知道如何处理Python字典和NumPy数组，没有什么新的学习。</span><span class="yiyi-st" id="yiyi-95">但是，由于要访问的数据的类型并不是事先知道的，直接使用标准操作符有一些优化的限制。</span><span class="yiyi-st" id="yiyi-96">对于生产代码，我们建议您利用本章中介绍的优化的熊猫数据访问方法。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-97">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-98">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-99">这有时被称为<code class="docutils literal"><span class="pre">链式</span><span class="pre">赋值</span></code>，应该避免。</span><span class="yiyi-st" id="yiyi-100">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-101">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-102">在0.15.0 <code class="docutils literal"><span class="pre">Index</span></code>内部被重构为不再子类化<code class="docutils literal"><span class="pre">ndarray</span></code>，而是子类化<code class="docutils literal"><span class="pre">PandasObject</span></code>，类似于其余的pandas对象。</span><span class="yiyi-st" id="yiyi-103">这应该是一个透明的变化，只有非常有限的API影响（参见<a class="reference internal" href="whatsnew.html#whatsnew-0150-refactoring"><span class="std std-ref">Internal Refactoring</span></a>）</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-104">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-105">对于具有浮点数的整数索引的索引已经在0.18.0中阐明，对于变化的总结，参见<a class="reference internal" href="whatsnew.html#whatsnew-0180-float-indexers"><span class="std std-ref">here</span></a>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-106">有关<code class="docutils literal"><span class="pre">MultiIndex</span></code>和更高级的索引文档，请参阅<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">MultiIndex / Advanced Indexing</span></a>。</span></p>
<p><span class="yiyi-st" id="yiyi-107">有关某些高级策略，请参阅<a class="reference internal" href="cookbook.html#cookbook-selection"><span class="std std-ref">cookbook</span></a></span></p>
<div class="section" id="different-choices-for-indexing">
<h2><span class="yiyi-st" id="yiyi-108">Different Choices for Indexing</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-109"><span class="versionmodified">版本0.11.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-110">对象选择已经有了许多用户请求的添加，以便支持更明确的基于位置的索引。</span><span class="yiyi-st" id="yiyi-111">pandas现在支持三种类型的多轴索引。</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-112"><code class="docutils literal"><span class="pre">.loc</span></code>主要是基于标签的，但也可以与布尔数组一起使用。</span><span class="yiyi-st" id="yiyi-113"><code class="docutils literal"><span class="pre">.loc</span></code>会在找不到项目时产生<code class="docutils literal"><span class="pre">KeyError</span></code>。</span><span class="yiyi-st" id="yiyi-114">允许输入为：</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-115">单个标签，例如<code class="docutils literal"><span class="pre">5</span></code>或<code class="docutils literal"><span class="pre">&apos;a&apos;</span></code>（注意，<code class="docutils literal"><span class="pre">5</span></code>被解释为索引的<em>标签</em>。</span><span class="yiyi-st" id="yiyi-116">这种使用是<strong>不是</strong>沿着索引的整数位置）</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-117">标签的列表或数组<code class="docutils literal"><span class="pre">[&apos;a&apos;，</span> <span class="pre">&apos;b&apos;，</span> <span class="pre">&apos;c&apos;]</span> </code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-118">具有标签<code class="docutils literal"><span class="pre">&apos;a&apos;:&apos;f&apos;</span></code>的切片对象（请注意，与通常的python切片相反，<strong></strong>包括开始和停止）</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-119">一个布尔数组</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-120">具有一个参数（调用Series，DataFrame或Panel）的<code class="docutils literal"><span class="pre">callable</span></code>函数，并返回有效的索引输出（上述之一）</span></p>
<blockquote>
<div><div class="versionadded">
<p><span class="yiyi-st" id="yiyi-121"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
</div></blockquote>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-122">有关详情，请参阅<a class="reference internal" href="#indexing-label"><span class="std std-ref">Selection by Label</span></a></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-123"><code class="docutils literal"><span class="pre">.iloc</span></code>主要是基于整数位置（从轴的<code class="docutils literal"><span class="pre">0</span></code>到<code class="docutils literal"><span class="pre">length-1</span></code>），但也可以与布尔数组一起使用。</span><span class="yiyi-st" id="yiyi-124">如果所请求的索引器超出边界，则<code class="docutils literal"><span class="pre">.iloc</span></code>将引发<code class="docutils literal"><span class="pre">IndexError</span></code>，除了允许超出索引的<em>slice</em>索引器。</span><span class="yiyi-st" id="yiyi-125">（这符合python / numpy <em>slice</em>语义）。</span><span class="yiyi-st" id="yiyi-126">允许输入为：</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-127">整数例如<code class="docutils literal"><span class="pre">5</span></code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-128">整数列表或数组<code class="docutils literal"><span class="pre">[4，</span> <span class="pre">3，</span> <span class="pre">0]</span></code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-129">int <code class="docutils literal"><span class="pre">1:7</span></code>的slice对象</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-130">一个布尔数组</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-131">具有一个参数（调用Series，DataFrame或Panel）的<code class="docutils literal"><span class="pre">callable</span></code>函数，并返回有效的索引输出（上述之一）</span></p>
<blockquote>
<div><div class="versionadded">
<p><span class="yiyi-st" id="yiyi-132"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
</div></blockquote>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-133">详情请参阅<a class="reference internal" href="#indexing-integer"><span class="std std-ref">Selection by Position</span></a></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-134"><code class="docutils literal"><span class="pre">.ix</span></code>支持混合整数和基于标签的访问。</span><span class="yiyi-st" id="yiyi-135">它主要是基于标签的，但将回落到整数位置访问，除非相应的轴是整数类型。</span><span class="yiyi-st" id="yiyi-136"><code class="docutils literal"><span class="pre">.ix</span></code>是最常用的，并且将支持<code class="docutils literal"><span class="pre">.loc</span></code>和<code class="docutils literal"><span class="pre">.iloc</span></code>中的任何输入。</span><span class="yiyi-st" id="yiyi-137"><code class="docutils literal"><span class="pre">.ix</span></code>也支持浮点标签方案。</span><span class="yiyi-st" id="yiyi-138"><code class="docutils literal"><span class="pre">.ix</span></code>在处理混合位置和基于标签的分层索引时非常有用。</span></p>
<p><span class="yiyi-st" id="yiyi-139">但是，当轴是基于整数的时，仅支持基于标签的访问，而不支持位置访问。</span><span class="yiyi-st" id="yiyi-140">因此，在这种情况下，通常最好是显式并使用<code class="docutils literal"><span class="pre">.iloc</span></code>或<code class="docutils literal"><span class="pre">.loc</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-141">详情请参阅<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">Advanced Indexing</span></a>和<a class="reference internal" href="advanced.html#advanced-advanced-hierarchical"><span class="std std-ref">Advanced Hierarchical</span></a>。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-142"><code class="docutils literal"><span class="pre">.loc</span></code>，<code class="docutils literal"><span class="pre">.iloc</span></code>，<code class="docutils literal"><span class="pre">.ix</span></code>以及<code class="docutils literal"><span class="pre">[]</span></code>索引可以接受<code class="docutils literal"><span class="pre">callable</span></code></span><span class="yiyi-st" id="yiyi-143">查看更多<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a>。</span></p>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-144">从多轴选择的对象获取值使用以下符号（使用<code class="docutils literal"><span class="pre">.loc</span></code>作为示例，但适用于<code class="docutils literal"><span class="pre">.iloc</span></code>和<code class="docutils literal"><span class="pre">.ix</span></code></span><span class="yiyi-st" id="yiyi-145">任何轴访问器可以是空片<code class="docutils literal"><span class="pre">:</span></code>。</span><span class="yiyi-st" id="yiyi-146">忽略规范的轴假定为<code class="docutils literal"><span class="pre">:</span></code>。</span><span class="yiyi-st" id="yiyi-147">（例如<code class="docutils literal"><span class="pre">p.loc[&apos;a&apos;]</span></code>等同于<code class="docutils literal"><span class="pre">p.loc [&apos;a&apos;，</span> <span class="pre">：，</span> <span class="pre">：]</span></code>）</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="38%">
<col width="63%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-148">对象类型</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-149">索引器</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-150">Series</span></td>
<td><span class="yiyi-st" id="yiyi-151"><code class="docutils literal"><span class="pre">s.loc[indexer]</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-152">DataFrame</span></td>
<td><span class="yiyi-st" id="yiyi-153"><code class="docutils literal"><span class="pre">df.loc[row_indexer,column_indexer]</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-154">Panel</span></td>
<td><span class="yiyi-st" id="yiyi-155"><code class="docutils literal"><span class="pre">p.loc[item_indexer,major_indexer,minor_indexer]</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="basics">
<span id="indexing-basics"></span><h2><span class="yiyi-st" id="yiyi-156">基础知识</span></h2>
<p><span class="yiyi-st" id="yiyi-157">如在在<a class="reference internal" href="basics.html#basics"><span class="std std-ref">last section</span></a>中引入数据结构时提到的，用<code class="docutils literal"><span class="pre">[]</span></code>（a.k.a.</span><span class="yiyi-st" id="yiyi-158"><code class="docutils literal"><span class="pre">__getitem__</span></code>用于熟悉在Python中实现类行为的人）选择低维切片。</span><span class="yiyi-st" id="yiyi-159">从而，</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="25%">
<col width="25%">
<col width="50%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-160">对象类型</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-161">选择</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-162">返回值类型</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-163">Series</span></td>
<td><span class="yiyi-st" id="yiyi-164"><code class="docutils literal"><span class="pre">series[label]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-165">标量值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-166">DataFrame</span></td>
<td><span class="yiyi-st" id="yiyi-167"><code class="docutils literal"><span class="pre">frame[colname]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-168"><code class="docutils literal"><span class="pre">Series</span></code>对应于colname</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-169">Panel</span></td>
<td><span class="yiyi-st" id="yiyi-170"><code class="docutils literal"><span class="pre">panel[itemname]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-171"><code class="docutils literal"><span class="pre">DataFrame</span></code>对应于项目名称</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-172">这里我们构造一个简单的时间序列数据集用于说明索引功能：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">dates</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>

<span class="gp">In [2]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">dates</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">])</span>

<span class="gp">In [3]: </span><span class="n">df</span>
<span class="gr">Out[3]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-04  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-05 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312  0.844885</span>

<span class="gp">In [4]: </span><span class="n">panel</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">({</span><span class="s1">&apos;one&apos;</span> <span class="p">:</span> <span class="n">df</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span> <span class="p">:</span> <span class="n">df</span> <span class="o">-</span> <span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">()})</span>

<span class="gp">In [5]: </span><span class="n">panel</span>
<span class="gr">Out[5]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 2 (items) x 8 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: one to two</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-08 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-173">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-174">除非特别声明，否则索引功能不是时间序列特定的。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-175">因此，如上所述，我们有使用<code class="docutils literal"><span class="pre">[]</span></code>的最基本的索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span>

<span class="gp">In [7]: </span><span class="n">s</span><span class="p">[</span><span class="n">dates</span><span class="p">[</span><span class="mi">5</span><span class="p">]]</span>
<span class="gr">Out[7]: </span><span class="o">-</span><span class="mf">0.67368970808837059</span>

<span class="gp">In [8]: </span><span class="n">panel</span><span class="p">[</span><span class="s1">&apos;two&apos;</span><span class="p">]</span>
<span class="gr">Out[8]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.409571  0.113086 -0.610826 -0.936507</span>
<span class="go">2000-01-02  1.152571  0.222735  1.017442 -0.845111</span>
<span class="go">2000-01-03 -0.921390 -1.708620  0.403304  1.270929</span>
<span class="go">2000-01-04  0.662014 -0.310822 -0.141342  0.470985</span>
<span class="go">2000-01-05 -0.484513  0.962970  1.174465 -0.888276</span>
<span class="go">2000-01-06 -0.733231  0.509598 -0.580194  0.724113</span>
<span class="go">2000-01-07  0.345164  0.972995 -0.816769 -0.840143</span>
<span class="go">2000-01-08 -0.430188 -0.761943 -0.446079  1.044010</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-176">您可以将列的列表传递到<code class="docutils literal"><span class="pre">[]</span></code>，以按该顺序选择列。</span><span class="yiyi-st" id="yiyi-177">如果DataFrame中不包含列，则会引发异常。</span><span class="yiyi-st" id="yiyi-178">也可以以这种方式设置多个列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [9]: </span><span class="n">df</span>
<span class="gr">Out[9]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-04  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-05 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312  0.844885</span>

<span class="gp">In [10]: </span><span class="n">df</span><span class="p">[[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;A&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>

<span class="gp">In [11]: </span><span class="n">df</span>
<span class="gr">Out[11]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -0.282863  0.469112 -1.509059 -1.135632</span>
<span class="go">2000-01-02 -0.173215  1.212112  0.119209 -1.044236</span>
<span class="go">2000-01-03 -2.104569 -0.861849 -0.494929  1.071804</span>
<span class="go">2000-01-04 -0.706771  0.721555 -1.039575  0.271860</span>
<span class="go">2000-01-05  0.567020 -0.424972  0.276232 -1.087401</span>
<span class="go">2000-01-06  0.113648 -0.673690 -1.478427  0.524988</span>
<span class="go">2000-01-07  0.577046  0.404705 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -1.157892 -0.370647 -1.344312  0.844885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-179">您可能会发现这对于将变换（就地）应用于列的子集很有用。</span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-180">警告</span></p>
<p><span class="yiyi-st" id="yiyi-181">pandas aligns all AXES when setting <code class="docutils literal"><span class="pre">Series</span></code> and <code class="docutils literal"><span class="pre">DataFrame</span></code> from <code class="docutils literal"><span class="pre">.loc</span></code>, <code class="docutils literal"><span class="pre">.iloc</span></code> and <code class="docutils literal"><span class="pre">.ix</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-182">这将<strong>不会</strong>修改<code class="docutils literal"><span class="pre">df</span></code>，因为列对齐在赋值之前。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>
<span class="gr">Out[12]: </span>
<span class="go">                   A         B</span>
<span class="go">2000-01-01 -0.282863  0.469112</span>
<span class="go">2000-01-02 -0.173215  1.212112</span>
<span class="go">2000-01-03 -2.104569 -0.861849</span>
<span class="go">2000-01-04 -0.706771  0.721555</span>
<span class="go">2000-01-05  0.567020 -0.424972</span>
<span class="go">2000-01-06  0.113648 -0.673690</span>
<span class="go">2000-01-07  0.577046  0.404705</span>
<span class="go">2000-01-08 -1.157892 -0.370647</span>

<span class="gp">In [13]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;A&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>

<span class="gp">In [14]: </span><span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>
<span class="gr">Out[14]: </span>
<span class="go">                   A         B</span>
<span class="go">2000-01-01 -0.282863  0.469112</span>
<span class="go">2000-01-02 -0.173215  1.212112</span>
<span class="go">2000-01-03 -2.104569 -0.861849</span>
<span class="go">2000-01-04 -0.706771  0.721555</span>
<span class="go">2000-01-05  0.567020 -0.424972</span>
<span class="go">2000-01-06  0.113648 -0.673690</span>
<span class="go">2000-01-07  0.577046  0.404705</span>
<span class="go">2000-01-08 -1.157892 -0.370647</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-183">正确的方法是使用原始值</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;A&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">values</span>

<span class="gp">In [16]: </span><span class="n">df</span><span class="p">[[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>
<span class="gr">Out[16]: </span>
<span class="go">                   A         B</span>
<span class="go">2000-01-01  0.469112 -0.282863</span>
<span class="go">2000-01-02  1.212112 -0.173215</span>
<span class="go">2000-01-03 -0.861849 -2.104569</span>
<span class="go">2000-01-04  0.721555 -0.706771</span>
<span class="go">2000-01-05 -0.424972  0.567020</span>
<span class="go">2000-01-06 -0.673690  0.113648</span>
<span class="go">2000-01-07  0.404705  0.577046</span>
<span class="go">2000-01-08 -0.370647 -1.157892</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="attribute-access">
<h2><span class="yiyi-st" id="yiyi-184">Attribute Access</span></h2>
<p id="indexing-attribute-access"><span class="yiyi-st" id="yiyi-185"><span id="indexing-df-cols"></span><span id="indexing-columns-multiple"></span>You may access an index on a <code class="docutils literal"><span class="pre">Series</span></code>, column on a <code class="docutils literal"><span class="pre">DataFrame</span></code>, and an item on a <code class="docutils literal"><span class="pre">Panel</span></code> directly as an attribute:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [17]: </span><span class="n">sa</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span><span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [18]: </span><span class="n">dfa</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [19]: </span><span class="n">sa</span><span class="o">.</span><span class="n">b</span>
<span class="gr">Out[19]: </span><span class="mi">2</span>

<span class="gp">In [20]: </span><span class="n">dfa</span><span class="o">.</span><span class="n">A</span>
<span class="gr">Out[20]: </span>
<span class="go">2000-01-01    0.469112</span>
<span class="go">2000-01-02    1.212112</span>
<span class="go">2000-01-03   -0.861849</span>
<span class="go">2000-01-04    0.721555</span>
<span class="go">2000-01-05   -0.424972</span>
<span class="go">2000-01-06   -0.673690</span>
<span class="go">2000-01-07    0.404705</span>
<span class="go">2000-01-08   -0.370647</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>

<span class="gp">In [21]: </span><span class="n">panel</span><span class="o">.</span><span class="n">one</span>
<span class="gr">Out[21]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-04  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-05 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312  0.844885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-186">您可以使用属性访问来修改DataFrame的系列或列的现有元素，但要小心；如果您尝试使用属性访问来创建新列，则会无声地失败，从而创建新属性而不是新列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="n">sa</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>

<span class="gp">In [23]: </span><span class="n">sa</span>
<span class="gr">Out[23]: </span>
<span class="go">a    5</span>
<span class="go">b    2</span>
<span class="go">c    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [24]: </span><span class="n">dfa</span><span class="o">.</span><span class="n">A</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dfa</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span>  <span class="c1"># ok if A already exists</span>

<span class="gp">In [25]: </span><span class="n">dfa</span>
<span class="gr">Out[25]: </span>
<span class="go">            A         B         C         D</span>
<span class="go">2000-01-01  0 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03  2 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-04  3 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-05  4  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-06  5  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-07  6  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08  7 -1.157892 -1.344312  0.844885</span>

<span class="gp">In [26]: </span><span class="n">dfa</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dfa</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span>  <span class="c1"># use this form to create a new column</span>

<span class="gp">In [27]: </span><span class="n">dfa</span>
<span class="gr">Out[27]: </span>
<span class="go">            A         B         C         D</span>
<span class="go">2000-01-01  0 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03  2 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-04  3 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-05  4  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-06  5  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-07  6  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08  7 -1.157892 -1.344312  0.844885</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-187">警告</span></p>
<ul class="last simple">
<li><span class="yiyi-st" id="yiyi-188">只有当索引元素是有效的Python标识符时，才能使用此访问。 <code class="docutils literal"><span class="pre">s.1</span></code>不允许。</span><span class="yiyi-st" id="yiyi-189">有关有效标识符的说明，请参见<a class="reference external" href="http://docs.python.org/2.7/reference/lexical_analysis.html#identifiers">。</a></span></li>
<li><span class="yiyi-st" id="yiyi-190">如果该属性与现有方法名称冲突，则该属性将不可用。 <code class="docutils literal"><span class="pre">s.min</span></code>不允许。</span></li>
<li><span class="yiyi-st" id="yiyi-191">Similarly, the attribute will not be available if it conflicts with any of the following list: <code class="docutils literal"><span class="pre">index</span></code>, <code class="docutils literal"><span class="pre">major_axis</span></code>, <code class="docutils literal"><span class="pre">minor_axis</span></code>, <code class="docutils literal"><span class="pre">items</span></code>, <code class="docutils literal"><span class="pre">labels</span></code>.</span></li>
<li><span class="yiyi-st" id="yiyi-192">在任何这些情况下，标准索引仍将工作，例如。 <code class="docutils literal"><span class="pre">s[&apos;1&apos;]</span></code>，<code class="docutils literal"><span class="pre">s[&apos;min&apos;]</span></code>和<code class="docutils literal"><span class="pre">s[&apos;index&apos;]</span></code>将访问相应的元素或列。</span></li>
<li><span class="yiyi-st" id="yiyi-193"><code class="docutils literal"><span class="pre">Series/Panel</span></code>访问从0.13.0开始提供。</span></li>
</ul>
</div>
<p><span class="yiyi-st" id="yiyi-194">如果使用IPython环境，您还可以使用制表符完成来查看这些可访问的属性。</span></p>
<p><span class="yiyi-st" id="yiyi-195">您还可以将<code class="docutils literal"><span class="pre">dict</span></code>分配给<code class="docutils literal"><span class="pre">DataFrame</span></code>的行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="n">x</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;x&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;y&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]})</span>

<span class="gp">In [29]: </span><span class="n">x</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">99</span><span class="p">)</span>

<span class="gp">In [30]: </span><span class="n">x</span>
<span class="gr">Out[30]: </span>
<span class="go">   x   y</span>
<span class="go">0  1   3</span>
<span class="go">1  9  99</span>
<span class="go">2  3   5</span>
</pre></div>
</div>
</div>
<div class="section" id="slicing-ranges">
<h2><span class="yiyi-st" id="yiyi-196">Slicing ranges</span></h2>
<p><span class="yiyi-st" id="yiyi-197">在<code class="docutils literal"><span class="pre">.iloc</span></code>方法的<a class="reference internal" href="#indexing-integer"><span class="std std-ref">Selection by Position</span></a>部分中描述了沿任意轴切分范围的最稳健和一致的方法。</span><span class="yiyi-st" id="yiyi-198">现在，我们使用<code class="docutils literal"><span class="pre">[]</span></code>运算符解释切片的语义。</span></p>
<p><span class="yiyi-st" id="yiyi-199">使用Series，语法与ndarray完全一样，返回值的一部分和相应的标签：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">s</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[31]: </span>
<span class="go">2000-01-01    0.469112</span>
<span class="go">2000-01-02    1.212112</span>
<span class="go">2000-01-03   -0.861849</span>
<span class="go">2000-01-04    0.721555</span>
<span class="go">2000-01-05   -0.424972</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>

<span class="gp">In [32]: </span><span class="n">s</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[32]: </span>
<span class="go">2000-01-01    0.469112</span>
<span class="go">2000-01-03   -0.861849</span>
<span class="go">2000-01-05   -0.424972</span>
<span class="go">2000-01-07    0.404705</span>
<span class="go">Freq: 2D, Name: A, dtype: float64</span>

<span class="gp">In [33]: </span><span class="n">s</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[33]: </span>
<span class="go">2000-01-08   -0.370647</span>
<span class="go">2000-01-07    0.404705</span>
<span class="go">2000-01-06   -0.673690</span>
<span class="go">2000-01-05   -0.424972</span>
<span class="go">2000-01-04    0.721555</span>
<span class="go">2000-01-03   -0.861849</span>
<span class="go">2000-01-02    1.212112</span>
<span class="go">2000-01-01    0.469112</span>
<span class="go">Freq: -1D, Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-200">请注意，也可以通过切片赋值：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">s2</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [35]: </span><span class="n">s2</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

<span class="gp">In [36]: </span><span class="n">s2</span>
<span class="gr">Out[36]: </span>
<span class="go">2000-01-01    0.000000</span>
<span class="go">2000-01-02    0.000000</span>
<span class="go">2000-01-03    0.000000</span>
<span class="go">2000-01-04    0.000000</span>
<span class="go">2000-01-05    0.000000</span>
<span class="go">2000-01-06   -0.673690</span>
<span class="go">2000-01-07    0.404705</span>
<span class="go">2000-01-08   -0.370647</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-201">使用DataFrame，在<code class="docutils literal"><span class="pre">[]</span></code> <strong>内切片将</strong>切片。</span><span class="yiyi-st" id="yiyi-202">这主要是为了方便，因为它是这样的常见操作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [37]: </span><span class="n">df</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[37]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804</span>

<span class="gp">In [38]: </span><span class="n">df</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[38]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312  0.844885</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-06 -0.673690  0.113648 -1.478427  0.524988</span>
<span class="go">2000-01-05 -0.424972  0.567020  0.276232 -1.087401</span>
<span class="go">2000-01-04  0.721555 -0.706771 -1.039575  0.271860</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-label">
<span id="indexing-label"></span><h2><span class="yiyi-st" id="yiyi-203">Selection By Label</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-204">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-205">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-206">这有时被称为<code class="docutils literal"><span class="pre">链接</span> <span class="pre">分配</span></code>，应该避免。</span><span class="yiyi-st" id="yiyi-207">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-208">警告</span></p>
<blockquote>
<div><span class="yiyi-st" id="yiyi-209"><code class="docutils literal"><span class="pre">.loc</span></code>在您呈现不兼容（或可转换）索引类型的切片器时是严格的。</span><span class="yiyi-st" id="yiyi-210">例如，在<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>中使用整数。</span><span class="yiyi-st" id="yiyi-211">这将产生<code class="docutils literal"><span class="pre">TypeError</span></code>。</span></div></blockquote>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="n">dfl</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABCD&apos;</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span><span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">))</span>

<span class="gp">In [40]: </span><span class="n">dfl</span>
<span class="gr">Out[40]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2013-01-01  1.075770 -0.109050  1.643563 -1.469388</span>
<span class="go">2013-01-02  0.357021 -0.674600 -1.776904 -0.968914</span>
<span class="go">2013-01-03 -1.294524  0.413738  0.276662 -0.472035</span>
<span class="go">2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061</span>
<span class="go">2013-01-05  0.895717  0.805244 -1.206412  2.565646</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="go">TypeError: cannot do slice indexing on &lt;class &apos;pandas.tseries.index.DatetimeIndex&apos;&gt; with these indexers [2] of &lt;type &apos;int&apos;&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-212">在切片<em>中的字符串喜欢</em>可以转换为索引的类型，并导致自然切片。</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;20130102&apos;</span><span class="p">:</span><span class="s1">&apos;20130104&apos;</span><span class="p">]</span>
<span class="gr">Out[41]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2013-01-02  0.357021 -0.674600 -1.776904 -0.968914</span>
<span class="go">2013-01-03 -1.294524  0.413738  0.276662 -0.472035</span>
<span class="go">2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-213">pandas提供了一套方法，以便<strong>完全基于标签的索引</strong>。</span><span class="yiyi-st" id="yiyi-214">这是一个严格的包含协议。</span><span class="yiyi-st" id="yiyi-215">您要求的标签的<strong>至少1个</strong>必须在索引中，否则会出现<code class="docutils literal"><span class="pre">KeyError</span></code>！</span><span class="yiyi-st" id="yiyi-216">当切片时，起始边界是<em>包括</em>，<strong>AND</strong>停止边界是<em>包括</em>。</span><span class="yiyi-st" id="yiyi-217">整数是有效标签，但它们指的是标签<strong>，而不是位置</strong>。</span></p>
<p><span class="yiyi-st" id="yiyi-218"><code class="docutils literal"><span class="pre">.loc</span></code>属性是主访问方法。</span><span class="yiyi-st" id="yiyi-219">以下是有效输入：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-220">单个标签，例如<code class="docutils literal"><span class="pre">5</span></code>或<code class="docutils literal"><span class="pre">&apos;a&apos;</span></code>（注意，<code class="docutils literal"><span class="pre">5</span></code>被解释为索引的<em>标签</em>。</span><span class="yiyi-st" id="yiyi-221">这种使用是<strong>不是</strong>沿着索引的整数位置）</span></li>
<li><span class="yiyi-st" id="yiyi-222">标签的列表或数组<code class="docutils literal"><span class="pre">[&apos;a&apos;，</span> <span class="pre">&apos;b&apos;，</span> <span class="pre">&apos;c&apos;]</span> </code></span></li>
<li><span class="yiyi-st" id="yiyi-223">具有标签<code class="docutils literal"><span class="pre">&apos;a&apos;:&apos;f&apos;</span></code>的切片对象（请注意，与通常的python切片相反，<strong></strong>包括开始和停止）</span></li>
<li><span class="yiyi-st" id="yiyi-224">一个布尔数组</span></li>
<li><span class="yiyi-st" id="yiyi-225">A <code class="docutils literal"><span class="pre">callable</span></code>，请参阅<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a></span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [42]: </span><span class="n">s1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">),</span><span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abcdef&apos;</span><span class="p">))</span>

<span class="gp">In [43]: </span><span class="n">s1</span>
<span class="gr">Out[43]: </span>
<span class="go">a    1.431256</span>
<span class="go">b    1.340309</span>
<span class="go">c   -1.170299</span>
<span class="go">d   -0.226169</span>
<span class="go">e    0.410835</span>
<span class="go">f    0.813850</span>
<span class="go">dtype: float64</span>

<span class="gp">In [44]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;c&apos;</span><span class="p">:]</span>
<span class="gr">Out[44]: </span>
<span class="go">c   -1.170299</span>
<span class="go">d   -0.226169</span>
<span class="go">e    0.410835</span>
<span class="go">f    0.813850</span>
<span class="go">dtype: float64</span>

<span class="gp">In [45]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;b&apos;</span><span class="p">]</span>
<span class="gr">Out[45]: </span><span class="mf">1.3403088497993827</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-226">请注意，设置也很好：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [46]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;c&apos;</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">0</span>

<span class="gp">In [47]: </span><span class="n">s1</span>
<span class="gr">Out[47]: </span>
<span class="go">a    1.431256</span>
<span class="go">b    1.340309</span>
<span class="go">c    0.000000</span>
<span class="go">d    0.000000</span>
<span class="go">e    0.000000</span>
<span class="go">f    0.000000</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-227">用DataFrame</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [48]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abcdef&apos;</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABCD&apos;</span><span class="p">))</span>
<span class="gp">   ....:</span> 

<span class="gp">In [49]: </span><span class="n">df1</span>
<span class="gr">Out[49]: </span>
<span class="go">          A         B         C         D</span>
<span class="go">a  0.132003 -0.827317 -0.076467 -1.187678</span>
<span class="go">b  1.130127 -1.436737 -1.413681  1.607920</span>
<span class="go">c  1.024180  0.569605  0.875906 -2.211372</span>
<span class="go">d  0.974466 -2.006747 -0.410001 -0.078638</span>
<span class="go">e  0.545952 -1.219217 -1.226825  0.769804</span>
<span class="go">f -1.281247 -0.727707 -0.121306 -0.097883</span>

<span class="gp">In [50]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">],</span> <span class="p">:]</span>
<span class="gr">Out[50]: </span>
<span class="go">          A         B         C         D</span>
<span class="go">a  0.132003 -0.827317 -0.076467 -1.187678</span>
<span class="go">b  1.130127 -1.436737 -1.413681  1.607920</span>
<span class="go">d  0.974466 -2.006747 -0.410001 -0.078638</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-228">通过标签切片访问</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;d&apos;</span><span class="p">:,</span> <span class="s1">&apos;A&apos;</span><span class="p">:</span><span class="s1">&apos;C&apos;</span><span class="p">]</span>
<span class="gr">Out[51]: </span>
<span class="go">          A         B         C</span>
<span class="go">d  0.974466 -2.006747 -0.410001</span>
<span class="go">e  0.545952 -1.219217 -1.226825</span>
<span class="go">f -1.281247 -0.727707 -0.121306</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-229">要获取使用标签的横截面（等效于<code class="docutils literal"><span class="pre">df.xs(&apos;a&apos;)</span></code>）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [52]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]</span>
<span class="gr">Out[52]: </span>
<span class="go">A    0.132003</span>
<span class="go">B   -0.827317</span>
<span class="go">C   -0.076467</span>
<span class="go">D   -1.187678</span>
<span class="go">Name: a, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-230">获取带有布尔数组的值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [53]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span>
<span class="gr">Out[53]: </span>
<span class="go">A     True</span>
<span class="go">B    False</span>
<span class="go">C    False</span>
<span class="go">D    False</span>
<span class="go">Name: a, dtype: bool</span>

<span class="gp">In [54]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[54]: </span>
<span class="go">          A</span>
<span class="go">a  0.132003</span>
<span class="go">b  1.130127</span>
<span class="go">c  1.024180</span>
<span class="go">d  0.974466</span>
<span class="go">e  0.545952</span>
<span class="go">f -1.281247</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-231">显式获取值（等同于弃用的<code class="docutils literal"><span class="pre">df.get_value(&apos;a&apos;,&apos;A&apos;)</span></code>）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># this is also equivalent to ``df1.at[&apos;a&apos;,&apos;A&apos;]``</span>
<span class="gp">In [55]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;A&apos;</span><span class="p">]</span>
<span class="gr">Out[55]: </span><span class="mf">0.13200317033032932</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-position">
<span id="indexing-integer"></span><h2><span class="yiyi-st" id="yiyi-232">Selection By Position</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-233">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-234">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-235">这有时被称为<code class="docutils literal"><span class="pre">链接</span> <span class="pre">分配</span></code>，应该避免。</span><span class="yiyi-st" id="yiyi-236">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-237">pandas提供了一套方法，以便获得<strong>纯粹基于整数的索引</strong>。</span><span class="yiyi-st" id="yiyi-238">语义遵循python和numpy切片。</span><span class="yiyi-st" id="yiyi-239">这些是基于<code class="docutils literal"><span class="pre">0-based</span></code>索引。</span><span class="yiyi-st" id="yiyi-240">当切片时，起始边界是<em>包括</em>，而上边界是<em>排除</em>。</span><span class="yiyi-st" id="yiyi-241">尝试使用非整数，即使是<strong>有效的</strong>标签也会产生<code class="docutils literal"><span class="pre">IndexError</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-242"><code class="docutils literal"><span class="pre">.iloc</span></code>属性是主要访问方法。</span><span class="yiyi-st" id="yiyi-243">以下是有效输入：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-244">整数例如<code class="docutils literal"><span class="pre">5</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-245">整数列表或数组<code class="docutils literal"><span class="pre">[4，</span> <span class="pre">3，</span> <span class="pre">0]</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-246">int <code class="docutils literal"><span class="pre">1:7</span></code>的slice对象</span></li>
<li><span class="yiyi-st" id="yiyi-247">一个布尔数组</span></li>
<li><span class="yiyi-st" id="yiyi-248">A <code class="docutils literal"><span class="pre">callable</span></code>，请参阅<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a></span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </span><span class="n">s1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>

<span class="gp">In [57]: </span><span class="n">s1</span>
<span class="gr">Out[57]: </span>
<span class="go">0    0.695775</span>
<span class="go">2    0.341734</span>
<span class="go">4    0.959726</span>
<span class="go">6   -1.110336</span>
<span class="go">8   -0.619976</span>
<span class="go">dtype: float64</span>

<span class="gp">In [58]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[58]: </span>
<span class="go">0    0.695775</span>
<span class="go">2    0.341734</span>
<span class="go">4    0.959726</span>
<span class="go">dtype: float64</span>

<span class="gp">In [59]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[59]: </span><span class="o">-</span><span class="mf">1.1103361028911669</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-249">请注意，iloc也可以用来赋值：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [60]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

<span class="gp">In [61]: </span><span class="n">s1</span>
<span class="gr">Out[61]: </span>
<span class="go">0    0.000000</span>
<span class="go">2    0.000000</span>
<span class="go">4    0.000000</span>
<span class="go">6   -1.110336</span>
<span class="go">8   -0.619976</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-250">用DataFrame</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">2</span><span class="p">)),</span>
<span class="gp">   ....:</span>                    <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="gp">   ....:</span> 

<span class="gp">In [63]: </span><span class="n">df1</span>
<span class="gr">Out[63]: </span>
<span class="go">           0         2         4         6</span>
<span class="go">0   0.149748 -0.732339  0.687738  0.176444</span>
<span class="go">2   0.403310 -0.154951  0.301624 -2.179861</span>
<span class="go">4  -1.369849 -0.954208  1.462696 -1.743161</span>
<span class="go">6  -0.826591 -0.345352  1.314232  0.690579</span>
<span class="go">8   0.995761  2.396780  0.014871  3.357427</span>
<span class="go">10 -0.317441 -1.236269  0.896171 -0.487602</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-251">通过整数切片选择</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[64]: </span>
<span class="go">          0         2         4         6</span>
<span class="go">0  0.149748 -0.732339  0.687738  0.176444</span>
<span class="go">2  0.403310 -0.154951  0.301624 -2.179861</span>
<span class="go">4 -1.369849 -0.954208  1.462696 -1.743161</span>

<span class="gp">In [65]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="gr">Out[65]: </span>
<span class="go">          4         6</span>
<span class="go">2  0.301624 -2.179861</span>
<span class="go">4  1.462696 -1.743161</span>
<span class="go">6  1.314232  0.690579</span>
<span class="go">8  0.014871  3.357427</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-252">通过整数列表选择</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [66]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]]</span>
<span class="gr">Out[66]: </span>
<span class="go">           2         6</span>
<span class="go">2  -0.154951 -2.179861</span>
<span class="go">6  -0.345352  0.690579</span>
<span class="go">10 -1.236269 -0.487602</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:]</span>
<span class="gr">Out[67]: </span>
<span class="go">          0         2         4         6</span>
<span class="go">2  0.403310 -0.154951  0.301624 -2.179861</span>
<span class="go">4 -1.369849 -0.954208  1.462696 -1.743161</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[68]: </span>
<span class="go">           2         4</span>
<span class="go">0  -0.732339  0.687738</span>
<span class="go">2  -0.154951  0.301624</span>
<span class="go">4  -0.954208  1.462696</span>
<span class="go">6  -0.345352  1.314232</span>
<span class="go">8   2.396780  0.014871</span>
<span class="go">10 -1.236269  0.896171</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># this is also equivalent to ``df1.iat[1,1]``</span>
<span class="gp">In [69]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[69]: </span><span class="o">-</span><span class="mf">0.15495077442490321</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-253">为了获得使用整数位置的横截面（等于<code class="docutils literal"><span class="pre">df.xs(1)</span></code>）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[70]: </span>
<span class="go">0    0.403310</span>
<span class="go">2   -0.154951</span>
<span class="go">4    0.301624</span>
<span class="go">6   -2.179861</span>
<span class="go">Name: 2, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-254">超出范围的切片索引会像在Python / Numpy中一样优雅地处理。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># these are allowed in python/numpy.</span>
<span class="c"># Only works in Pandas starting from v0.14.0.</span>
<span class="gp">In [71]: </span><span class="n">x</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abcdef&apos;</span><span class="p">)</span>

<span class="gp">In [72]: </span><span class="n">x</span>
<span class="gr">Out[72]: </span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;f&apos;</span><span class="p">]</span>

<span class="gp">In [73]: </span><span class="n">x</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[73]: </span><span class="p">[</span><span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;f&apos;</span><span class="p">]</span>

<span class="gp">In [74]: </span><span class="n">x</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[74]: </span><span class="p">[]</span>

<span class="gp">In [75]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

<span class="gp">In [76]: </span><span class="n">s</span>
<span class="gr">Out[76]: </span>
<span class="go">0    a</span>
<span class="go">1    b</span>
<span class="go">2    c</span>
<span class="go">3    d</span>
<span class="go">4    e</span>
<span class="go">5    f</span>
<span class="go">dtype: object</span>

<span class="gp">In [77]: </span><span class="n">s</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[77]: </span>
<span class="go">4    e</span>
<span class="go">5    f</span>
<span class="go">dtype: object</span>

<span class="gp">In [78]: </span><span class="n">s</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[78]: </span><span class="n">Series</span><span class="p">([],</span> <span class="n">dtype</span><span class="p">:</span> <span class="nb">object</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-255">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-256">在v0.14.0之前，<code class="docutils literal"><span class="pre">iloc</span></code>将不接受切片的边界索引器，例如。超过被索引的对象的长度的值。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-257">请注意，这可能会导致空的轴（例如返回一个空DataFrame）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [79]: </span><span class="n">dfl</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;AB&apos;</span><span class="p">))</span>

<span class="gp">In [80]: </span><span class="n">dfl</span>
<span class="gr">Out[80]: </span>
<span class="go">          A         B</span>
<span class="go">0 -0.082240 -2.182937</span>
<span class="go">1  0.380396  0.084844</span>
<span class="go">2  0.432390  1.519970</span>
<span class="go">3 -0.493662  0.600178</span>
<span class="go">4  0.274230  0.132885</span>

<span class="gp">In [81]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="mi">2</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[81]: </span>
<span class="go">Empty DataFrame</span>
<span class="go">Columns: []</span>
<span class="go">Index: [0, 1, 2, 3, 4]</span>

<span class="gp">In [82]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[82]: </span>
<span class="go">          B</span>
<span class="go">0 -2.182937</span>
<span class="go">1  0.084844</span>
<span class="go">2  1.519970</span>
<span class="go">3  0.600178</span>
<span class="go">4  0.132885</span>

<span class="gp">In [83]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">6</span><span class="p">]</span>
<span class="gr">Out[83]: </span>
<span class="go">         A         B</span>
<span class="go">4  0.27423  0.132885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-258">超出范围的单个索引器将生成<code class="docutils literal"><span class="pre">IndexError</span></code>。</span><span class="yiyi-st" id="yiyi-259">任何元素超出边界的索引器列表将生成<code class="docutils literal"><span class="pre">IndexError</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span>
<span class="ne">IndexError</span><span class="p">:</span> <span class="n">positional</span> <span class="n">indexers</span> <span class="n">are</span> <span class="n">out</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">bounds</span>

<span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="mi">4</span><span class="p">]</span>
<span class="ne">IndexError</span><span class="p">:</span> <span class="n">single</span> <span class="n">positional</span> <span class="n">indexer</span> <span class="ow">is</span> <span class="n">out</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">bounds</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-callable">
<span id="indexing-callable"></span><h2><span class="yiyi-st" id="yiyi-260">Selection By Callable</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-261"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-262"><code class="docutils literal"><span class="pre">.loc</span></code>，<code class="docutils literal"><span class="pre">.iloc</span></code>，<code class="docutils literal"><span class="pre">.ix</span></code>以及<code class="docutils literal"><span class="pre">[]</span></code>索引可以接受<code class="docutils literal"><span class="pre">callable</span></code></span><span class="yiyi-st" id="yiyi-263"><code class="docutils literal"><span class="pre">callable</span></code>必须是具有一个参数（调用Series，DataFrame或Panel）的函数，并返回有效的索引输出。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abcdef&apos;</span><span class="p">),</span>
<span class="gp">   ....:</span>                    <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABCD&apos;</span><span class="p">))</span>
<span class="gp">   ....:</span> 

<span class="gp">In [85]: </span><span class="n">df1</span>
<span class="gr">Out[85]: </span>
<span class="go">          A         B         C         D</span>
<span class="go">a -0.023688  2.410179  1.450520  0.206053</span>
<span class="go">b -0.251905 -2.213588  1.063327  1.266143</span>
<span class="go">c  0.299368 -0.863838  0.408204 -1.048089</span>
<span class="go">d -0.025747 -0.988387  0.094055  1.262731</span>
<span class="go">e  1.289997  0.082423 -0.055758  0.536580</span>
<span class="go">f -0.489682  0.369374 -0.034571 -2.484478</span>

<span class="gp">In [86]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">A</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="p">:]</span>
<span class="gr">Out[86]: </span>
<span class="go">          A         B         C         D</span>
<span class="go">c  0.299368 -0.863838  0.408204 -1.048089</span>
<span class="go">e  1.289997  0.082423 -0.055758  0.536580</span>

<span class="gp">In [87]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">]]</span>
<span class="gr">Out[87]: </span>
<span class="go">          A         B</span>
<span class="go">a -0.023688  2.410179</span>
<span class="go">b -0.251905 -2.213588</span>
<span class="go">c  0.299368 -0.863838</span>
<span class="go">d -0.025747 -0.988387</span>
<span class="go">e  1.289997  0.082423</span>
<span class="go">f -0.489682  0.369374</span>

<span class="gp">In [88]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
<span class="gr">Out[88]: </span>
<span class="go">          A         B</span>
<span class="go">a -0.023688  2.410179</span>
<span class="go">b -0.251905 -2.213588</span>
<span class="go">c  0.299368 -0.863838</span>
<span class="go">d -0.025747 -0.988387</span>
<span class="go">e  1.289997  0.082423</span>
<span class="go">f -0.489682  0.369374</span>

<span class="gp">In [89]: </span><span class="n">df1</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
<span class="gr">Out[89]: </span>
<span class="go">a   -0.023688</span>
<span class="go">b   -0.251905</span>
<span class="go">c    0.299368</span>
<span class="go">d   -0.025747</span>
<span class="go">e    1.289997</span>
<span class="go">f   -0.489682</span>
<span class="go">Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-264">您可以在<code class="docutils literal"><span class="pre">Series</span></code>中使用可调用索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [90]: </span><span class="n">df1</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[90]: </span>
<span class="go">c    0.299368</span>
<span class="go">e    1.289997</span>
<span class="go">Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-265">使用这些方法/索引器，您可以链接数据选择操作，而不使用临时变量。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [91]: </span><span class="n">bb</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;data/baseball.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s1">&apos;id&apos;</span><span class="p">)</span>

<span class="gp">In [92]: </span><span class="p">(</span><span class="n">bb</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">&apos;year&apos;</span><span class="p">,</span> <span class="s1">&apos;team&apos;</span><span class="p">])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gp">   ....:</span>    <span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">r</span> <span class="o">&gt;</span> <span class="mi">100</span><span class="p">])</span>
<span class="gp">   ....:</span> 
<span class="gr">Out[92]: </span>
<span class="go">           stint    g    ab    r    h  X2b  X3b  hr    rbi    sb   cs   bb  \</span>
<span class="go">year team                                                                    </span>
<span class="go">2007 CIN       6  379   745  101  203   35    2  36  125.0  10.0  1.0  105   </span>
<span class="go">     DET       5  301  1062  162  283   54    4  37  144.0  24.0  7.0   97   </span>
<span class="go">     HOU       4  311   926  109  218   47    6  14   77.0  10.0  4.0   60   </span>
<span class="go">     LAN      11  413  1021  153  293   61    3  36  154.0   7.0  5.0  114   </span>
<span class="go">     NYN      13  622  1854  240  509  101    3  61  243.0  22.0  4.0  174   </span>
<span class="go">     SFN       5  482  1305  198  337   67    6  40  171.0  26.0  7.0  235   </span>
<span class="go">     TEX       2  198   729  115  200   40    4  28  115.0  21.0  4.0   73   </span>
<span class="go">     TOR       4  459  1408  187  378   96    2  58  223.0   4.0  2.0  190   </span>

<span class="go">              so   ibb   hbp    sh    sf  gidp  </span>
<span class="go">year team                                       </span>
<span class="go">2007 CIN   127.0  14.0   1.0   1.0  15.0  18.0  </span>
<span class="go">     DET   176.0   3.0  10.0   4.0   8.0  28.0  </span>
<span class="go">     HOU   212.0   3.0   9.0  16.0   6.0  17.0  </span>
<span class="go">     LAN   141.0   8.0   9.0   3.0   8.0  29.0  </span>
<span class="go">     NYN   310.0  24.0  23.0  18.0  15.0  48.0  </span>
<span class="go">     SFN   188.0  51.0   8.0  16.0   6.0  41.0  </span>
<span class="go">     TEX   140.0   4.0   5.0   2.0   8.0  16.0  </span>
<span class="go">     TOR   265.0  16.0  12.0   4.0  16.0  38.0  </span>
</pre></div>
</div>
</div>
<div class="section" id="selecting-random-samples">
<span id="indexing-basics-partial-setting"></span><h2><span class="yiyi-st" id="yiyi-266">Selecting Random Samples</span></h2>
<p><span class="yiyi-st" id="yiyi-267">使用<a class="reference internal" href="generated/pandas.DataFrame.sample.html#pandas.DataFrame.sample" title="pandas.DataFrame.sample"><code class="xref py py-meth docutils literal"><span class="pre">sample()</span></code></a>方法从Series，DataFrame或Panel随机选择行或列。</span><span class="yiyi-st" id="yiyi-268">该方法将默认对行进行抽样，并接受要返回的特定行数/列数，或一小部分行数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [93]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>

<span class="c"># When no arguments are passed, returns 1 row.</span>
<span class="gp">In [94]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">()</span>
<span class="gr">Out[94]: </span>
<span class="go">4    4</span>
<span class="go">dtype: int64</span>

<span class="c"># One may specify either a number of rows:</span>
<span class="gp">In [95]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[95]: </span>
<span class="go">0    0</span>
<span class="go">4    4</span>
<span class="go">1    1</span>
<span class="go">dtype: int64</span>

<span class="c"># Or a fraction of the rows:</span>
<span class="gp">In [96]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">frac</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="gr">Out[96]: </span>
<span class="go">5    5</span>
<span class="go">3    3</span>
<span class="go">1    1</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-269">默认情况下，<code class="docutils literal"><span class="pre">sample</span></code>最多只返回一行，但也可以使用<code class="docutils literal"><span class="pre">replace</span></code>选项表示能否重复抽取：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [97]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>

<span class="go"> # Without replacement (default):</span>
<span class="gp">In [98]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[98]: </span>
<span class="go">0    0</span>
<span class="go">1    1</span>
<span class="go">5    5</span>
<span class="go">3    3</span>
<span class="go">2    2</span>
<span class="go">4    4</span>
<span class="go">dtype: int64</span>

<span class="go"> # With replacement:</span>
<span class="gp">In [99]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[99]: </span>
<span class="go">0    0</span>
<span class="go">4    4</span>
<span class="go">3    3</span>
<span class="go">2    2</span>
<span class="go">4    4</span>
<span class="go">4    4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-270">默认情况下，每行具有相等的选择概率，但如果您希望行具有不同的概率，则可以将<code class="docutils literal"><span class="pre">sample</span></code>函数抽样权重作为<code class="docutils literal"><span class="pre">weights</span></code>。</span><span class="yiyi-st" id="yiyi-271">这些权重可以是列表，numpy数组或系列，但它们必须与要采样的对象长度相同。</span><span class="yiyi-st" id="yiyi-272">缺少的值将被视为零的权重，不允许使用inf值。</span><span class="yiyi-st" id="yiyi-273">如果权重不等于1，则它们将通过将所有权重除以权重的总和而被重新归一化。</span><span class="yiyi-st" id="yiyi-274">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [100]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>

<span class="gp">In [101]: </span><span class="n">example_weights</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">]</span>

<span class="gp">In [102]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">example_weights</span><span class="p">)</span>
<span class="gr">Out[102]: </span>
<span class="go">5    5</span>
<span class="go">4    4</span>
<span class="go">3    3</span>
<span class="go">dtype: int64</span>

<span class="c"># Weights will be re-normalized automatically</span>
<span class="gp">In [103]: </span><span class="n">example_weights2</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>

<span class="gp">In [104]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">example_weights2</span><span class="p">)</span>
<span class="gr">Out[104]: </span>
<span class="go">0    0</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-275">应用于DataFrame时，只需将列的名称作为字符串传递，就可以使用DataFrame的列作为抽样权重（前提是您正在对行进行抽样而不是列）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [105]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col1&apos;</span><span class="p">:[</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="s1">&apos;weight_column&apos;</span><span class="p">:[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]})</span>

<span class="gp">In [106]: </span><span class="n">df2</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="n">weights</span> <span class="o">=</span> <span class="s1">&apos;weight_column&apos;</span><span class="p">)</span>
<span class="gr">Out[106]: </span>
<span class="go">   col1  weight_column</span>
<span class="go">1     8            0.4</span>
<span class="go">0     9            0.5</span>
<span class="go">2     7            0.1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-276"><code class="docutils literal"><span class="pre">sample</span></code>还允许用户使用<code class="docutils literal"><span class="pre">axis</span></code>参数对列而不是行进行抽样。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [107]: </span><span class="n">df3</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col1&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;col2&apos;</span><span class="p">:[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]})</span>

<span class="gp">In [108]: </span><span class="n">df3</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[108]: </span>
<span class="go">   col1</span>
<span class="go">0     1</span>
<span class="go">1     2</span>
<span class="go">2     3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-277">最后，还可以使用<code class="docutils literal"><span class="pre">random_state</span></code>参数为<code class="docutils literal"><span class="pre">sample</span></code>的随机数生成器设置种子，它将接受整数（作为种子）或numpy RandomState对象。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [109]: </span><span class="n">df4</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col1&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;col2&apos;</span><span class="p">:[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]})</span>

<span class="c"># With a given seed, the sample will always draw the same rows.</span>
<span class="gp">In [110]: </span><span class="n">df4</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[110]: </span>
<span class="go">   col1  col2</span>
<span class="go">2     3     4</span>
<span class="go">1     2     3</span>

<span class="gp">In [111]: </span><span class="n">df4</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[111]: </span>
<span class="go">   col1  col2</span>
<span class="go">2     3     4</span>
<span class="go">1     2     3</span>
</pre></div>
</div>
</div>
<div class="section" id="setting-with-enlargement">
<h2><span class="yiyi-st" id="yiyi-278">Setting With Enlargement</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-279"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-280">当为该轴设置不存在的键时，<code class="docutils literal"><span class="pre">.loc/.ix/[]</span></code>操作可以执行放大。</span></p>
<p><span class="yiyi-st" id="yiyi-281">在<code class="docutils literal"><span class="pre">Series</span></code>情况下，这实际上是一个附加操作</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [112]: </span><span class="n">se</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>

<span class="gp">In [113]: </span><span class="n">se</span>
<span class="gr">Out[113]: </span>
<span class="go">0    1</span>
<span class="go">1    2</span>
<span class="go">2    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [114]: </span><span class="n">se</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="mf">5.</span>

<span class="gp">In [115]: </span><span class="n">se</span>
<span class="gr">Out[115]: </span>
<span class="go">0    1.0</span>
<span class="go">1    2.0</span>
<span class="go">2    3.0</span>
<span class="go">5    5.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-282">可以通过<code class="docutils literal"><span class="pre">.loc</span></code>在任一轴上放大<code class="docutils literal"><span class="pre">DataFrame</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [116]: </span><span class="n">dfi</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span>
<span class="gp">   .....:</span>                 <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [117]: </span><span class="n">dfi</span>
<span class="gr">Out[117]: </span>
<span class="go">   A  B</span>
<span class="go">0  0  1</span>
<span class="go">1  2  3</span>
<span class="go">2  4  5</span>

<span class="gp">In [118]: </span><span class="n">dfi</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span><span class="s1">&apos;C&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dfi</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span><span class="s1">&apos;A&apos;</span><span class="p">]</span>

<span class="gp">In [119]: </span><span class="n">dfi</span>
<span class="gr">Out[119]: </span>
<span class="go">   A  B  C</span>
<span class="go">0  0  1  0</span>
<span class="go">1  2  3  2</span>
<span class="go">2  4  5  4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-283">这类似于<code class="docutils literal"><span class="pre">DataFrame</span></code>上的<code class="docutils literal"><span class="pre">append</span></code>操作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [120]: </span><span class="n">dfi</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>

<span class="gp">In [121]: </span><span class="n">dfi</span>
<span class="gr">Out[121]: </span>
<span class="go">   A  B  C</span>
<span class="go">0  0  1  0</span>
<span class="go">1  2  3  2</span>
<span class="go">2  4  5  4</span>
<span class="go">3  5  5  5</span>
</pre></div>
</div>
</div>
<div class="section" id="fast-scalar-value-getting-and-setting">
<span id="indexing-basics-get-value"></span><h2><span class="yiyi-st" id="yiyi-284">Fast scalar value getting and setting</span></h2>
<p><span class="yiyi-st" id="yiyi-285">由于索引与<code class="docutils literal"><span class="pre">[]</span></code>必须处理很多情况（单标签访问，切片，布尔索引等）。</span><span class="yiyi-st" id="yiyi-286">），它有一点开销，以找出你要求什么。</span><span class="yiyi-st" id="yiyi-287">如果只想访问标量值，最快的方法是使用<code class="docutils literal"><span class="pre">at</span></code>和<code class="docutils literal"><span class="pre">iat</span></code>方法，这是在所有的数据结构上实现的。</span></p>
<p><span class="yiyi-st" id="yiyi-288">Similarly to <code class="docutils literal"><span class="pre">loc</span></code>, <code class="docutils literal"><span class="pre">at</span></code> provides <strong>label</strong> based scalar lookups, while, <code class="docutils literal"><span class="pre">iat</span></code> provides <strong>integer</strong> based lookups analogously to <code class="docutils literal"><span class="pre">iloc</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [122]: </span><span class="n">s</span><span class="o">.</span><span class="n">iat</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[122]: </span><span class="mi">5</span>

<span class="gp">In [123]: </span><span class="n">df</span><span class="o">.</span><span class="n">at</span><span class="p">[</span><span class="n">dates</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="s1">&apos;A&apos;</span><span class="p">]</span>
<span class="gr">Out[123]: </span><span class="o">-</span><span class="mf">0.67368970808837059</span>

<span class="gp">In [124]: </span><span class="n">df</span><span class="o">.</span><span class="n">iat</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[124]: </span><span class="mf">0.72155516224436689</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-289">您还可以使用这些相同的索引器进行设置。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [125]: </span><span class="n">df</span><span class="o">.</span><span class="n">at</span><span class="p">[</span><span class="n">dates</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="s1">&apos;E&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">7</span>

<span class="gp">In [126]: </span><span class="n">df</span><span class="o">.</span><span class="n">iat</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">7</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-290">如果索引器丢失，<code class="docutils literal"><span class="pre">at</span></code>可以如上所述就地放大对象。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [127]: </span><span class="n">df</span><span class="o">.</span><span class="n">at</span><span class="p">[</span><span class="n">dates</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">7</span>

<span class="gp">In [128]: </span><span class="n">df</span>
<span class="gr">Out[128]: </span>
<span class="go">                   A         B         C         D    E    0</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632  NaN  NaN</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236  NaN  NaN</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929  1.071804  NaN  NaN</span>
<span class="go">2000-01-04  7.000000 -0.706771 -1.039575  0.271860  NaN  NaN</span>
<span class="go">2000-01-05 -0.424972  0.567020  0.276232 -1.087401  NaN  NaN</span>
<span class="go">2000-01-06 -0.673690  0.113648 -1.478427  0.524988  7.0  NaN</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268  NaN  NaN</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312  0.844885  NaN  NaN</span>
<span class="go">2000-01-09       NaN       NaN       NaN       NaN  NaN  7.0</span>
</pre></div>
</div>
</div>
<div class="section" id="boolean-indexing">
<h2><span class="yiyi-st" id="yiyi-291">Boolean indexing</span></h2>
<p id="indexing-boolean"><span class="yiyi-st" id="yiyi-292">另一个常见的操作是使用布尔向量来过滤数据。</span><span class="yiyi-st" id="yiyi-293">The operators are: <code class="docutils literal"><span class="pre">|</span></code> for <code class="docutils literal"><span class="pre">or</span></code>, <code class="docutils literal"><span class="pre">&amp;</span></code> for <code class="docutils literal"><span class="pre">and</span></code>, and <code class="docutils literal"><span class="pre">~</span></code> for <code class="docutils literal"><span class="pre">not</span></code>. </span><span class="yiyi-st" id="yiyi-294">这些<strong>必须</strong>使用括号分组。</span></p>
<p><span class="yiyi-st" id="yiyi-295">使用布尔向量索引一个系列的工作原理与numpy ndarray完全一样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [129]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>

<span class="gp">In [130]: </span><span class="n">s</span>
<span class="gr">Out[130]: </span>
<span class="go">0   -3</span>
<span class="go">1   -2</span>
<span class="go">2   -1</span>
<span class="go">3    0</span>
<span class="go">4    1</span>
<span class="go">5    2</span>
<span class="go">6    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [131]: </span><span class="n">s</span><span class="p">[</span><span class="n">s</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[131]: </span>
<span class="go">4    1</span>
<span class="go">5    2</span>
<span class="go">6    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [132]: </span><span class="n">s</span><span class="p">[(</span><span class="n">s</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">s</span> <span class="o">&gt;</span> <span class="mf">0.5</span><span class="p">)]</span>
<span class="gr">Out[132]: </span>
<span class="go">0   -3</span>
<span class="go">1   -2</span>
<span class="go">4    1</span>
<span class="go">5    2</span>
<span class="go">6    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [133]: </span><span class="n">s</span><span class="p">[</span><span class="o">~</span><span class="p">(</span><span class="n">s</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)]</span>
<span class="gr">Out[133]: </span>
<span class="go">3    0</span>
<span class="go">4    1</span>
<span class="go">5    2</span>
<span class="go">6    3</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-296">您可以使用与DataFrame的索引长度相同的布尔向量从DataFrame中选择行（例如，从DataFrame的某个列派生的内容）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [134]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[134]: </span>
<span class="go">                   A         B         C         D   E   0</span>
<span class="go">2000-01-01  0.469112 -0.282863 -1.509059 -1.135632 NaN NaN</span>
<span class="go">2000-01-02  1.212112 -0.173215  0.119209 -1.044236 NaN NaN</span>
<span class="go">2000-01-04  7.000000 -0.706771 -1.039575  0.271860 NaN NaN</span>
<span class="go">2000-01-07  0.404705  0.577046 -1.715002 -1.039268 NaN NaN</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-297">列表推导和系列的<code class="docutils literal"><span class="pre">map</span></code>方法也可用于产生更复杂的标准：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [135]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;three&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;six&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;b&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;y&apos;</span><span class="p">,</span> <span class="s1">&apos;y&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;y&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;c&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">7</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="c"># only want &apos;two&apos; or &apos;three&apos;</span>
<span class="gp">In [136]: </span><span class="n">criterion</span> <span class="o">=</span> <span class="n">df2</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&apos;t&apos;</span><span class="p">))</span>

<span class="gp">In [137]: </span><span class="n">df2</span><span class="p">[</span><span class="n">criterion</span><span class="p">]</span>
<span class="gr">Out[137]: </span>
<span class="go">       a  b         c</span>
<span class="go">2    two  y  0.041290</span>
<span class="go">3  three  x  0.361719</span>
<span class="go">4    two  y -0.238075</span>

<span class="c"># equivalent but slower</span>
<span class="gp">In [138]: </span><span class="n">df2</span><span class="p">[[</span><span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&apos;t&apos;</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df2</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]]]</span>
<span class="gr">Out[138]: </span>
<span class="go">       a  b         c</span>
<span class="go">2    two  y  0.041290</span>
<span class="go">3  three  x  0.361719</span>
<span class="go">4    two  y -0.238075</span>

<span class="c"># Multiple criteria</span>
<span class="gp">In [139]: </span><span class="n">df2</span><span class="p">[</span><span class="n">criterion</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;b&apos;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&apos;x&apos;</span><span class="p">)]</span>
<span class="gr">Out[139]: </span>
<span class="go">       a  b         c</span>
<span class="go">3  three  x  0.361719</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-298">注意，选择方法<a class="reference internal" href="#indexing-label"><span class="std std-ref">Selection by Label</span></a>，<a class="reference internal" href="#indexing-integer"><span class="std std-ref">Selection by Position</span></a>和<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">Advanced Indexing</span></a>可以使用布尔向量组合选择多个轴与其他索引表达式。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [140]: </span><span class="n">df2</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">criterion</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;b&apos;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&apos;x&apos;</span><span class="p">),</span><span class="s1">&apos;b&apos;</span><span class="p">:</span><span class="s1">&apos;c&apos;</span><span class="p">]</span>
<span class="gr">Out[140]: </span>
<span class="go">   b         c</span>
<span class="go">3  x  0.361719</span>
</pre></div>
</div>
</div>
<div class="section" id="indexing-with-isin">
<span id="indexing-basics-indexing-isin"></span><h2><span class="yiyi-st" id="yiyi-299">Indexing with isin</span></h2>
<p><span class="yiyi-st" id="yiyi-300">考虑Series的<code class="docutils literal"><span class="pre">isin</span></code>方法，它返回一个布尔向量，它在传递列表中存在Series元素的地方。</span><span class="yiyi-st" id="yiyi-301">这允许您选择一个或多个列具有所需值的行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [141]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [142]: </span><span class="n">s</span>
<span class="gr">Out[142]: </span>
<span class="go">4    0</span>
<span class="go">3    1</span>
<span class="go">2    2</span>
<span class="go">1    3</span>
<span class="go">0    4</span>
<span class="go">dtype: int64</span>

<span class="gp">In [143]: </span><span class="n">s</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="gr">Out[143]: </span>
<span class="go">4    False</span>
<span class="go">3    False</span>
<span class="go">2     True</span>
<span class="go">1    False</span>
<span class="go">0     True</span>
<span class="go">dtype: bool</span>

<span class="gp">In [144]: </span><span class="n">s</span><span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">])]</span>
<span class="gr">Out[144]: </span>
<span class="go">2    2</span>
<span class="go">0    4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-302">对于<code class="docutils literal"><span class="pre">Index</span></code>对象也可以使用相同的方法，并且当您不知道哪些查找的标签实际上存在时非常有用：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [145]: </span><span class="n">s</span><span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">])]</span>
<span class="gr">Out[145]: </span>
<span class="go">4    0</span>
<span class="go">2    2</span>
<span class="go">dtype: int64</span>

<span class="c"># compare it to the following</span>
<span class="gp">In [146]: </span><span class="n">s</span><span class="p">[[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span>
<span class="gr">Out[146]: </span>
<span class="go">2    2.0</span>
<span class="go">4    0.0</span>
<span class="go">6    NaN</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-303">除此之外，<code class="docutils literal"><span class="pre">MultiIndex</span></code>允许选择一个单独的级别在成员资格检查中使用：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [147]: </span><span class="n">s_mi</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">),</span>
<span class="gp">   .....:</span>                  <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">]]))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [148]: </span><span class="n">s_mi</span>
<span class="gr">Out[148]: </span>
<span class="go">0  a    0</span>
<span class="go">   b    1</span>
<span class="go">   c    2</span>
<span class="go">1  a    3</span>
<span class="go">   b    4</span>
<span class="go">   c    5</span>
<span class="go">dtype: int64</span>

<span class="gp">In [149]: </span><span class="n">s_mi</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">s_mi</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">isin</span><span class="p">([(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">)])]</span>
<span class="gr">Out[149]: </span>
<span class="go">0  c    2</span>
<span class="go">1  a    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [150]: </span><span class="n">s_mi</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="n">s_mi</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">],</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)]</span>
<span class="gr">Out[150]: </span>
<span class="go">0  a    0</span>
<span class="go">   c    2</span>
<span class="go">1  a    3</span>
<span class="go">   c    5</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-304">DataFrame也有一个<code class="docutils literal"><span class="pre">isin</span></code>方法。</span><span class="yiyi-st" id="yiyi-305">当调用<code class="docutils literal"><span class="pre">isin</span></code>时，传递一组值作为数组或dict。</span><span class="yiyi-st" id="yiyi-306">如果值是数组，则<code class="docutils literal"><span class="pre">isin</span></code>返回与原始DataFrame形状相同的布尔数据框架，如果元素位于值序列中，则返回True。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [151]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;vals&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="s1">&apos;ids&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;f&apos;</span><span class="p">,</span> <span class="s1">&apos;n&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;ids2&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;n&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;n&apos;</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [152]: </span><span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>

<span class="gp">In [153]: </span><span class="n">df</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
<span class="gr">Out[153]: </span>
<span class="go">     ids   ids2   vals</span>
<span class="go">0   True   True   True</span>
<span class="go">1   True  False  False</span>
<span class="go">2  False  False   True</span>
<span class="go">3  False  False  False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-307">通常，您需要将某些值与某些列匹配。</span><span class="yiyi-st" id="yiyi-308">只需将值设置为<code class="docutils literal"><span class="pre">dict</span></code>，其中键是列，值是要检查的项目列表。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [154]: </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;ids&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="s1">&apos;vals&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>

<span class="gp">In [155]: </span><span class="n">df</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
<span class="gr">Out[155]: </span>
<span class="go">     ids   ids2   vals</span>
<span class="go">0   True  False   True</span>
<span class="go">1   True  False  False</span>
<span class="go">2  False  False   True</span>
<span class="go">3  False  False  False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-309">将DataFrame的<code class="docutils literal"><span class="pre">isin</span></code>与<code class="docutils literal"><span class="pre">any()</span></code>和<code class="docutils literal"><span class="pre">all()</span></code>组合，以快速选择满足给定条件的数据子集。</span><span class="yiyi-st" id="yiyi-310">要选择每个列满足其自己的条件的行：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [156]: </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&apos;ids&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="s1">&apos;ids2&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">],</span> <span class="s1">&apos;vals&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">]}</span>

<span class="gp">In [157]: </span><span class="n">row_mask</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">values</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="gp">In [158]: </span><span class="n">df</span><span class="p">[</span><span class="n">row_mask</span><span class="p">]</span>
<span class="gr">Out[158]: </span>
<span class="go">  ids ids2  vals</span>
<span class="go">0   a    a     1</span>
</pre></div>
</div>
</div>
<div class="section" id="the-where-method-and-masking">
<span id="indexing-where-mask"></span><h2><span class="yiyi-st" id="yiyi-311">The <a class="reference internal" href="generated/pandas.DataFrame.where.html#pandas.DataFrame.where" title="pandas.DataFrame.where"><code class="xref py py-meth docutils literal"><span class="pre">where()</span></code></a> Method and Masking</span></h2>
<p><span class="yiyi-st" id="yiyi-312">使用布尔向量从系列中选择值通常返回数据的子集。</span><span class="yiyi-st" id="yiyi-313">为了确保选择输出具有与原始数据相同的形状，可以使用<code class="docutils literal"><span class="pre">Series</span></code>和<code class="docutils literal"><span class="pre">DataFrame</span></code>中的<code class="docutils literal"><span class="pre">where</span></code>方法。</span></p>
<p><span class="yiyi-st" id="yiyi-314">仅返回所选行</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [159]: </span><span class="n">s</span><span class="p">[</span><span class="n">s</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[159]: </span>
<span class="go">3    1</span>
<span class="go">2    2</span>
<span class="go">1    3</span>
<span class="go">0    4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-315">返回与原始形状相同的系列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [160]: </span><span class="n">s</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">s</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[160]: </span>
<span class="go">4    NaN</span>
<span class="go">3    1.0</span>
<span class="go">2    2.0</span>
<span class="go">1    3.0</span>
<span class="go">0    4.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-316">使用布尔标准从DataFrame中选择值现在也保留输入数据形状。</span><span class="yiyi-st" id="yiyi-317"><code class="docutils literal"><span class="pre">where</span></code>在引擎下用作实现。</span><span class="yiyi-st" id="yiyi-318">等价于<code class="docutils literal"><span class="pre">df.其中（df</span> <span class="pre"> <span class="pre">0）</span></span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [161]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[161]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -1.309525       NaN       NaN</span>
<span class="go">2000-01-02 -0.352480       NaN -1.192319       NaN</span>
<span class="go">2000-01-03 -0.864883       NaN -0.227870       NaN</span>
<span class="go">2000-01-04       NaN -1.222082       NaN -1.233203</span>
<span class="go">2000-01-05       NaN -0.605656 -1.169184       NaN</span>
<span class="go">2000-01-06       NaN -0.948458       NaN -0.684718</span>
<span class="go">2000-01-07 -2.670153 -0.114722       NaN -0.048048</span>
<span class="go">2000-01-08       NaN       NaN -0.048788 -0.808838</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-319">此外，<code class="docutils literal"><span class="pre">where</span></code>在返回的副本中采用可选的<code class="docutils literal"><span class="pre">other</span></code>参数替换条件为False的值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [162]: </span><span class="n">df</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[162]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -1.309525 -0.485855 -0.245166</span>
<span class="go">2000-01-02 -0.352480 -0.390389 -1.192319 -1.655824</span>
<span class="go">2000-01-03 -0.864883 -0.299674 -0.227870 -0.281059</span>
<span class="go">2000-01-04 -0.846958 -1.222082 -0.600705 -1.233203</span>
<span class="go">2000-01-05 -0.669692 -0.605656 -1.169184 -0.342416</span>
<span class="go">2000-01-06 -0.868584 -0.948458 -2.297780 -0.684718</span>
<span class="go">2000-01-07 -2.670153 -0.114722 -0.168904 -0.048048</span>
<span class="go">2000-01-08 -0.801196 -1.392071 -0.048788 -0.808838</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-320">您可能希望基于一些布尔值标准设置值。</span><span class="yiyi-st" id="yiyi-321">这可以直观地这样做：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [163]: </span><span class="n">s2</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [164]: </span><span class="n">s2</span><span class="p">[</span><span class="n">s2</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

<span class="gp">In [165]: </span><span class="n">s2</span>
<span class="gr">Out[165]: </span>
<span class="go">4    0</span>
<span class="go">3    1</span>
<span class="go">2    2</span>
<span class="go">1    3</span>
<span class="go">0    4</span>
<span class="go">dtype: int64</span>

<span class="gp">In [166]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [167]: </span><span class="n">df2</span><span class="p">[</span><span class="n">df2</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

<span class="gp">In [168]: </span><span class="n">df2</span>
<span class="gr">Out[168]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.000000  0.000000  0.485855  0.245166</span>
<span class="go">2000-01-02  0.000000  0.390389  0.000000  1.655824</span>
<span class="go">2000-01-03  0.000000  0.299674  0.000000  0.281059</span>
<span class="go">2000-01-04  0.846958  0.000000  0.600705  0.000000</span>
<span class="go">2000-01-05  0.669692  0.000000  0.000000  0.342416</span>
<span class="go">2000-01-06  0.868584  0.000000  2.297780  0.000000</span>
<span class="go">2000-01-07  0.000000  0.000000  0.168904  0.000000</span>
<span class="go">2000-01-08  0.801196  1.392071  0.000000  0.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-322">默认情况下，<code class="docutils literal"><span class="pre">where</span></code>返回数据的修改副本。</span><span class="yiyi-st" id="yiyi-323">有一个可选参数<code class="docutils literal"><span class="pre">inplace</span></code>，以便可以在不创建副本的情况下修改原始数据：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [169]: </span><span class="n">df_orig</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [170]: </span><span class="n">df_orig</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">df</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="bp">True</span><span class="p">);</span>

<span class="gp">In [171]: </span><span class="n">df_orig</span>
<span class="gr">Out[171]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  2.104139  1.309525  0.485855  0.245166</span>
<span class="go">2000-01-02  0.352480  0.390389  1.192319  1.655824</span>
<span class="go">2000-01-03  0.864883  0.299674  0.227870  0.281059</span>
<span class="go">2000-01-04  0.846958  1.222082  0.600705  1.233203</span>
<span class="go">2000-01-05  0.669692  0.605656  1.169184  0.342416</span>
<span class="go">2000-01-06  0.868584  0.948458  2.297780  0.684718</span>
<span class="go">2000-01-07  2.670153  0.114722  0.168904  0.048048</span>
<span class="go">2000-01-08  0.801196  1.392071  0.048788  0.808838</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-324">注意</span></p>
<p><span class="yiyi-st" id="yiyi-325"><a class="reference internal" href="generated/pandas.DataFrame.where.html#pandas.DataFrame.where" title="pandas.DataFrame.where"><code class="xref py py-func docutils literal"><span class="pre">DataFrame.where()</span></code></a>的签名与<a class="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html#numpy.where" title="(in NumPy v1.11)"><code class="xref py py-func docutils literal"><span class="pre">numpy.where()</span></code></a>不同。</span><span class="yiyi-st" id="yiyi-326">Roughly <code class="docutils literal"><span class="pre">df1.where(m,</span> <span class="pre">df2)</span></code> is equivalent to <code class="docutils literal"><span class="pre">np.where(m,</span> <span class="pre">df1,</span> <span class="pre">df2)</span></code>.</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [172]: </span><span class="n">df</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="n">df</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">,</span> <span class="n">df</span><span class="p">,</span> <span class="o">-</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[172]: </span>
<span class="go">               A     B     C     D</span>
<span class="go">2000-01-01  True  True  True  True</span>
<span class="go">2000-01-02  True  True  True  True</span>
<span class="go">2000-01-03  True  True  True  True</span>
<span class="go">2000-01-04  True  True  True  True</span>
<span class="go">2000-01-05  True  True  True  True</span>
<span class="go">2000-01-06  True  True  True  True</span>
<span class="go">2000-01-07  True  True  True  True</span>
<span class="go">2000-01-08  True  True  True  True</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-327"><strong>alignment</strong></span></p>
<p><span class="yiyi-st" id="yiyi-328">此外，<code class="docutils literal"><span class="pre">where</span></code>对齐输入布尔条件（ndarray或DataFrame），以便可以进行带设置的部分选择。</span><span class="yiyi-st" id="yiyi-329">这类似于通过<code class="docutils literal"><span class="pre">.ix</span></code>（但是在内容而不是轴标签上）的部分设置，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [173]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [174]: </span><span class="n">df2</span><span class="p">[</span> <span class="n">df2</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>

<span class="gp">In [175]: </span><span class="n">df2</span>
<span class="gr">Out[175]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -1.309525  0.485855  0.245166</span>
<span class="go">2000-01-02 -0.352480  3.000000 -1.192319  3.000000</span>
<span class="go">2000-01-03 -0.864883  3.000000 -0.227870  3.000000</span>
<span class="go">2000-01-04  3.000000 -1.222082  3.000000 -1.233203</span>
<span class="go">2000-01-05  0.669692 -0.605656 -1.169184  0.342416</span>
<span class="go">2000-01-06  0.868584 -0.948458  2.297780 -0.684718</span>
<span class="go">2000-01-07 -2.670153 -0.114722  0.168904 -0.048048</span>
<span class="go">2000-01-08  0.801196  1.392071 -0.048788 -0.808838</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-330"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-331">在执行<code class="docutils literal"><span class="pre">where</span></code>时，还可以接受<code class="docutils literal"><span class="pre">axis</span></code>和<code class="docutils literal"><span class="pre">level</span></code>参数来对齐输入。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [176]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [177]: </span><span class="n">df2</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">df2</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">,</span><span class="n">df2</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span><span class="n">axis</span><span class="o">=</span><span class="s1">&apos;index&apos;</span><span class="p">)</span>
<span class="gr">Out[177]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -2.104139  0.485855  0.245166</span>
<span class="go">2000-01-02 -0.352480  0.390389 -0.352480  1.655824</span>
<span class="go">2000-01-03 -0.864883  0.299674 -0.864883  0.281059</span>
<span class="go">2000-01-04  0.846958  0.846958  0.600705  0.846958</span>
<span class="go">2000-01-05  0.669692  0.669692  0.669692  0.342416</span>
<span class="go">2000-01-06  0.868584  0.868584  2.297780  0.868584</span>
<span class="go">2000-01-07 -2.670153 -2.670153  0.168904 -2.670153</span>
<span class="go">2000-01-08  0.801196  1.392071  0.801196  0.801196</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-332">这是等价的（但比以下更快）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [178]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [179]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">x</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">,</span><span class="n">y</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">])</span>
<span class="gr">Out[179]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -2.104139  0.485855  0.245166</span>
<span class="go">2000-01-02 -0.352480  0.390389 -0.352480  1.655824</span>
<span class="go">2000-01-03 -0.864883  0.299674 -0.864883  0.281059</span>
<span class="go">2000-01-04  0.846958  0.846958  0.600705  0.846958</span>
<span class="go">2000-01-05  0.669692  0.669692  0.669692  0.342416</span>
<span class="go">2000-01-06  0.868584  0.868584  2.297780  0.868584</span>
<span class="go">2000-01-07 -2.670153 -2.670153  0.168904 -2.670153</span>
<span class="go">2000-01-08  0.801196  1.392071  0.801196  0.801196</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-333"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-334">在哪里可以接受可调用的条件和<code class="docutils literal"><span class="pre">other</span></code>参数。</span><span class="yiyi-st" id="yiyi-335">该函数必须带有一个参数（调用Series或DataFrame），并返回有效的输出作为条件和<code class="docutils literal"><span class="pre">other</span></code>参数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [180]: </span><span class="n">df3</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;A&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;B&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;C&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [181]: </span><span class="n">df3</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">4</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">10</span><span class="p">)</span>
<span class="gr">Out[181]: </span>
<span class="go">    A   B  C</span>
<span class="go">0  11  14  7</span>
<span class="go">1  12   5  8</span>
<span class="go">2  13   6  9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-336"><strong>掩码</strong></span></p>
<p><span class="yiyi-st" id="yiyi-337"><code class="docutils literal"><span class="pre">mask</span></code>是<code class="docutils literal"><span class="pre">where</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [182]: </span><span class="n">s</span><span class="o">.</span><span class="n">mask</span><span class="p">(</span><span class="n">s</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[182]: </span>
<span class="go">4   NaN</span>
<span class="go">3   NaN</span>
<span class="go">2   NaN</span>
<span class="go">1   NaN</span>
<span class="go">0   NaN</span>
<span class="go">dtype: float64</span>

<span class="gp">In [183]: </span><span class="n">df</span><span class="o">.</span><span class="n">mask</span><span class="p">(</span><span class="n">df</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[183]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -2.104139 -1.309525       NaN       NaN</span>
<span class="go">2000-01-02 -0.352480       NaN -1.192319       NaN</span>
<span class="go">2000-01-03 -0.864883       NaN -0.227870       NaN</span>
<span class="go">2000-01-04       NaN -1.222082       NaN -1.233203</span>
<span class="go">2000-01-05       NaN -0.605656 -1.169184       NaN</span>
<span class="go">2000-01-06       NaN -0.948458       NaN -0.684718</span>
<span class="go">2000-01-07 -2.670153 -0.114722       NaN -0.048048</span>
<span class="go">2000-01-08       NaN       NaN -0.048788 -0.808838</span>
</pre></div>
</div>
</div>
<div class="section" id="the-query-method-experimental">
<span id="indexing-query"></span><h2><span class="yiyi-st" id="yiyi-338">The <a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a> Method (Experimental)</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-339"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-340"><a class="reference internal" href="generated/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame"><code class="xref py py-class docutils literal"><span class="pre">DataFrame</span></code></a>对象具有允许使用表达式进行选择的<a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a>方法。</span></p>
<p><span class="yiyi-st" id="yiyi-341">您可以获取列<code class="docutils literal"><span class="pre">b</span></code>在列<code class="docutils literal"><span class="pre">a</span></code>和<code class="docutils literal"><span class="pre">c</span></code>之间的值的帧的值。例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [184]: </span><span class="n">n</span> <span class="o">=</span> <span class="mi">10</span>

<span class="gp">In [185]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [186]: </span><span class="n">df</span>
<span class="gr">Out[186]: </span>
<span class="go">          a         b         c</span>
<span class="go">0  0.438921  0.118680  0.863670</span>
<span class="go">1  0.138138  0.577363  0.686602</span>
<span class="go">2  0.595307  0.564592  0.520630</span>
<span class="go">3  0.913052  0.926075  0.616184</span>
<span class="go">4  0.078718  0.854477  0.898725</span>
<span class="go">5  0.076404  0.523211  0.591538</span>
<span class="go">6  0.792342  0.216974  0.564056</span>
<span class="go">7  0.397890  0.454131  0.915716</span>
<span class="go">8  0.074315  0.437913  0.019794</span>
<span class="go">9  0.559209  0.502065  0.026437</span>

<span class="c"># pure python</span>
<span class="gp">In [187]: </span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">b</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">c</span><span class="p">)]</span>
<span class="gr">Out[187]: </span>
<span class="go">          a         b         c</span>
<span class="go">1  0.138138  0.577363  0.686602</span>
<span class="go">4  0.078718  0.854477  0.898725</span>
<span class="go">5  0.076404  0.523211  0.591538</span>
<span class="go">7  0.397890  0.454131  0.915716</span>

<span class="c"># query</span>
<span class="gp">In [188]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;(a &lt; b) &amp; (b &lt; c)&apos;</span><span class="p">)</span>
<span class="gr">Out[188]: </span>
<span class="go">          a         b         c</span>
<span class="go">1  0.138138  0.577363  0.686602</span>
<span class="go">4  0.078718  0.854477  0.898725</span>
<span class="go">5  0.076404  0.523211  0.591538</span>
<span class="go">7  0.397890  0.454131  0.915716</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-342">如果没有名称为<code class="docutils literal"><span class="pre">a</span></code>的列，请执行相同的操作，但返回到命名的索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [189]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">n</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;bc&apos;</span><span class="p">))</span>

<span class="gp">In [190]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&apos;a&apos;</span>

<span class="gp">In [191]: </span><span class="n">df</span>
<span class="gr">Out[191]: </span>
<span class="go">   b  c</span>
<span class="go">a      </span>
<span class="go">0  0  4</span>
<span class="go">1  0  1</span>
<span class="go">2  3  4</span>
<span class="go">3  4  3</span>
<span class="go">4  1  4</span>
<span class="go">5  0  3</span>
<span class="go">6  0  1</span>
<span class="go">7  3  4</span>
<span class="go">8  2  3</span>
<span class="go">9  1  1</span>

<span class="gp">In [192]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &lt; b and b &lt; c&apos;</span><span class="p">)</span>
<span class="gr">Out[192]: </span>
<span class="go">   b  c</span>
<span class="go">a      </span>
<span class="go">2  3  4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-343">如果您不想或不能为索引命名，则可以在查询表达式中使用名称<code class="docutils literal"><span class="pre">index</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [193]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;bc&apos;</span><span class="p">))</span>

<span class="gp">In [194]: </span><span class="n">df</span>
<span class="gr">Out[194]: </span>
<span class="go">   b  c</span>
<span class="go">0  3  1</span>
<span class="go">1  3  0</span>
<span class="go">2  5  6</span>
<span class="go">3  5  2</span>
<span class="go">4  7  4</span>
<span class="go">5  0  1</span>
<span class="go">6  2  5</span>
<span class="go">7  0  1</span>
<span class="go">8  6  0</span>
<span class="go">9  7  9</span>

<span class="gp">In [195]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;index &lt; b &lt; c&apos;</span><span class="p">)</span>
<span class="gr">Out[195]: </span>
<span class="go">   b  c</span>
<span class="go">2  5  6</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-344">注意</span></p>
<p><span class="yiyi-st" id="yiyi-345">如果索引的名称与列名称重叠，则列名称将优先。</span><span class="yiyi-st" id="yiyi-346">例如，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [196]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">5</span><span class="p">)})</span>

<span class="gp">In [197]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&apos;a&apos;</span>

<span class="gp">In [198]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &gt; 2&apos;</span><span class="p">)</span> <span class="c1"># uses the column &apos;a&apos;, not the index</span>
<span class="gr">Out[198]: </span>
<span class="go">   a</span>
<span class="go">a   </span>
<span class="go">1  3</span>
<span class="go">3  3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-347">您仍然可以通过使用特殊标识符“index”在查询表达式中使用索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [199]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;index &gt; 2&apos;</span><span class="p">)</span>
<span class="gr">Out[199]: </span>
<span class="go">   a</span>
<span class="go">a   </span>
<span class="go">3  3</span>
<span class="go">4  2</span>
</pre></div>
</div>
<p class="last"><span class="yiyi-st" id="yiyi-348">如果由于某种原因你有一个名为<code class="docutils literal"><span class="pre">index</span></code>的列，那么你也可以将索引称为<code class="docutils literal"><span class="pre">ilevel_0</span></code>，但在这一点上你应该考虑将列重命名为暧昧。</span></p>
</div>
<div class="section" id="multiindex-query-syntax">
<h3><span class="yiyi-st" id="yiyi-349"><a class="reference internal" href="generated/pandas.MultiIndex.html#pandas.MultiIndex" title="pandas.MultiIndex"><code class="xref py py-class docutils literal"><span class="pre">MultiIndex</span></code></a> <a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a> Syntax</span></h3>
<p><span class="yiyi-st" id="yiyi-350">您还可以使用<code class="docutils literal"><span class="pre">DataFrame</span></code>和<a class="reference internal" href="generated/pandas.MultiIndex.html#pandas.MultiIndex" title="pandas.MultiIndex"><code class="xref py py-class docutils literal"><span class="pre">MultiIndex</span></code></a>的级别，如同它们是框架中的列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [200]: </span><span class="n">n</span> <span class="o">=</span> <span class="mi">10</span>

<span class="gp">In [201]: </span><span class="n">colors</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="s1">&apos;red&apos;</span><span class="p">,</span> <span class="s1">&apos;green&apos;</span><span class="p">],</span> <span class="n">size</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>

<span class="gp">In [202]: </span><span class="n">foods</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="s1">&apos;eggs&apos;</span><span class="p">,</span> <span class="s1">&apos;ham&apos;</span><span class="p">],</span> <span class="n">size</span><span class="o">=</span><span class="n">n</span><span class="p">)</span>

<span class="gp">In [203]: </span><span class="n">colors</span>
<span class="gr">Out[203]: </span>
<span class="go">array([&apos;red&apos;, &apos;red&apos;, &apos;red&apos;, &apos;green&apos;, &apos;green&apos;, &apos;green&apos;, &apos;green&apos;, &apos;green&apos;,</span>
<span class="go">       &apos;green&apos;, &apos;green&apos;], </span>
<span class="go">      dtype=&apos;|S5&apos;)</span>

<span class="gp">In [204]: </span><span class="n">foods</span>
<span class="gr">Out[204]: </span>
<span class="go">array([&apos;ham&apos;, &apos;ham&apos;, &apos;eggs&apos;, &apos;eggs&apos;, &apos;eggs&apos;, &apos;ham&apos;, &apos;ham&apos;, &apos;eggs&apos;, &apos;eggs&apos;,</span>
<span class="go">       &apos;eggs&apos;], </span>
<span class="go">      dtype=&apos;|S4&apos;)</span>

<span class="gp">In [205]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_arrays</span><span class="p">([</span><span class="n">colors</span><span class="p">,</span> <span class="n">foods</span><span class="p">],</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;color&apos;</span><span class="p">,</span> <span class="s1">&apos;food&apos;</span><span class="p">])</span>

<span class="gp">In [206]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">)</span>

<span class="gp">In [207]: </span><span class="n">df</span>
<span class="gr">Out[207]: </span>
<span class="go">                   0         1</span>
<span class="go">color food                    </span>
<span class="go">red   ham   0.194889 -0.381994</span>
<span class="go">      ham   0.318587  2.089075</span>
<span class="go">      eggs -0.728293 -0.090255</span>
<span class="go">green eggs -0.748199  1.318931</span>
<span class="go">      eggs -2.029766  0.792652</span>
<span class="go">      ham   0.461007 -0.542749</span>
<span class="go">      ham  -0.305384 -0.479195</span>
<span class="go">      eggs  0.095031 -0.270099</span>
<span class="go">      eggs -0.707140 -0.773882</span>
<span class="go">      eggs  0.229453  0.304418</span>

<span class="gp">In [208]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;color == &quot;red&quot;&apos;</span><span class="p">)</span>
<span class="gr">Out[208]: </span>
<span class="go">                   0         1</span>
<span class="go">color food                    </span>
<span class="go">red   ham   0.194889 -0.381994</span>
<span class="go">      ham   0.318587  2.089075</span>
<span class="go">      eggs -0.728293 -0.090255</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-351">如果<code class="docutils literal"><span class="pre">MultiIndex</span></code>的级别未命名，则可以使用特殊名称引用它们：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [209]: </span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="bp">None</span><span class="p">]</span>

<span class="gp">In [210]: </span><span class="n">df</span>
<span class="gr">Out[210]: </span>
<span class="go">                   0         1</span>
<span class="go">red   ham   0.194889 -0.381994</span>
<span class="go">      ham   0.318587  2.089075</span>
<span class="go">      eggs -0.728293 -0.090255</span>
<span class="go">green eggs -0.748199  1.318931</span>
<span class="go">      eggs -2.029766  0.792652</span>
<span class="go">      ham   0.461007 -0.542749</span>
<span class="go">      ham  -0.305384 -0.479195</span>
<span class="go">      eggs  0.095031 -0.270099</span>
<span class="go">      eggs -0.707140 -0.773882</span>
<span class="go">      eggs  0.229453  0.304418</span>

<span class="gp">In [211]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;ilevel_0 == &quot;red&quot;&apos;</span><span class="p">)</span>
<span class="gr">Out[211]: </span>
<span class="go">                 0         1</span>
<span class="go">red ham   0.194889 -0.381994</span>
<span class="go">    ham   0.318587  2.089075</span>
<span class="go">    eggs -0.728293 -0.090255</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-352">约定是<code class="docutils literal"><span class="pre">ilevel_0</span></code>，这意味着<code class="docutils literal"><span class="pre">index</span></code>的第0级的“索引级别0”。</span></p>
</div>
<div class="section" id="query-use-cases">
<h3><span class="yiyi-st" id="yiyi-353"><a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a> Use Cases</span></h3>
<p><span class="yiyi-st" id="yiyi-354"><a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a>的用例是当您拥有一个具有共同的列名（或索引级别/名称）子集的<a class="reference internal" href="generated/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame"><code class="xref py py-class docutils literal"><span class="pre">DataFrame</span></code></a>对象的集合时。</span><span class="yiyi-st" id="yiyi-355">您可以向<em>两个框架传递相同的查询，而不必</em>指定您感兴趣的框架</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [212]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [213]: </span><span class="n">df</span>
<span class="gr">Out[213]: </span>
<span class="go">          a         b         c</span>
<span class="go">0  0.224283  0.736107  0.139168</span>
<span class="go">1  0.302827  0.657803  0.713897</span>
<span class="go">2  0.611185  0.136624  0.984960</span>
<span class="go">3  0.195246  0.123436  0.627712</span>
<span class="go">4  0.618673  0.371660  0.047902</span>
<span class="go">5  0.480088  0.062993  0.185760</span>
<span class="go">6  0.568018  0.483467  0.445289</span>
<span class="go">7  0.309040  0.274580  0.587101</span>
<span class="go">8  0.258993  0.477769  0.370255</span>
<span class="go">9  0.550459  0.840870  0.304611</span>

<span class="gp">In [214]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span> <span class="o">+</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span>

<span class="gp">In [215]: </span><span class="n">df2</span>
<span class="gr">Out[215]: </span>
<span class="go">           a         b         c</span>
<span class="go">0   0.357579  0.229800  0.596001</span>
<span class="go">1   0.309059  0.957923  0.965663</span>
<span class="go">2   0.123102  0.336914  0.318616</span>
<span class="go">3   0.526506  0.323321  0.860813</span>
<span class="go">4   0.518736  0.486514  0.384724</span>
<span class="go">5   0.190804  0.505723  0.614533</span>
<span class="go">6   0.891939  0.623977  0.676639</span>
<span class="go">7   0.480559  0.378528  0.460858</span>
<span class="go">8   0.420223  0.136404  0.141295</span>
<span class="go">9   0.732206  0.419540  0.604675</span>
<span class="go">10  0.604466  0.848974  0.896165</span>
<span class="go">11  0.589168  0.920046  0.732716</span>

<span class="gp">In [216]: </span><span class="n">expr</span> <span class="o">=</span> <span class="s1">&apos;0.0 &lt;= a &lt;= c &lt;= 0.5&apos;</span>

<span class="gp">In [217]: </span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">frame</span><span class="p">:</span> <span class="n">frame</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">expr</span><span class="p">),</span> <span class="p">[</span><span class="n">df</span><span class="p">,</span> <span class="n">df2</span><span class="p">])</span>
<span class="gr">Out[217]: </span>
<span class="go">[          a         b         c</span>
<span class="go"> 8  0.258993  0.477769  0.370255,           a         b         c</span>
<span class="go"> 2  0.123102  0.336914  0.318616]</span>
</pre></div>
</div>
</div>
<div class="section" id="query-python-versus-pandas-syntax-comparison">
<h3><span class="yiyi-st" id="yiyi-356"><a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a> Python versus pandas Syntax Comparison</span></h3>
<p><span class="yiyi-st" id="yiyi-357">类似numpy的语法</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [218]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">3</span><span class="p">)),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [219]: </span><span class="n">df</span>
<span class="gr">Out[219]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>
<span class="go">1  1  0  7</span>
<span class="go">2  2  7  2</span>
<span class="go">3  6  2  2</span>
<span class="go">4  2  6  3</span>
<span class="go">5  3  8  2</span>
<span class="go">6  1  7  2</span>
<span class="go">7  5  1  5</span>
<span class="go">8  9  8  0</span>
<span class="go">9  1  5  0</span>

<span class="gp">In [220]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;(a &lt; b) &amp; (b &lt; c)&apos;</span><span class="p">)</span>
<span class="gr">Out[220]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>

<span class="gp">In [221]: </span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">b</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">c</span><span class="p">)]</span>
<span class="gr">Out[221]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-358">通过删除括号略微更好一些（通过绑定使比较运算符绑定比<code class="docutils literal"><span class="pre">&amp;</span></code> / <code class="docutils literal"><span class="pre">|</span></code>更紧密）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [222]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &lt; b &amp; b &lt; c&apos;</span><span class="p">)</span>
<span class="gr">Out[222]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-359">使用英语而不是符号</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [223]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &lt; b and b &lt; c&apos;</span><span class="p">)</span>
<span class="gr">Out[223]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-360">很接近你怎么可能写在纸上</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [224]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &lt; b &lt; c&apos;</span><span class="p">)</span>
<span class="gr">Out[224]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  7  8  9</span>
</pre></div>
</div>
</div>
<div class="section" id="the-in-and-not-in-operators">
<h3><span class="yiyi-st" id="yiyi-361">The <code class="docutils literal"><span class="pre">in</span></code> and <code class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></code> operators</span></h3>
<p><span class="yiyi-st" id="yiyi-362"><a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a> also supports special use of Python’s <code class="docutils literal"><span class="pre">in</span></code> and <code class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></code> comparison operators, providing a succinct syntax for calling the <code class="docutils literal"><span class="pre">isin</span></code> method of a <code class="docutils literal"><span class="pre">Series</span></code> or <code class="docutils literal"><span class="pre">DataFrame</span></code>.</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># get all rows where columns &quot;a&quot; and &quot;b&quot; have overlapping values</span>
<span class="gp">In [225]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;aabbccddeeff&apos;</span><span class="p">),</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;aaaabbbbcccc&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">12</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;d&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">12</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [226]: </span><span class="n">df</span>
<span class="gr">Out[226]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">1   a  a  4  7</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">6   d  b  3  3</span>
<span class="go">7   d  b  2  1</span>
<span class="go">8   e  c  4  3</span>
<span class="go">9   e  c  2  0</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>

<span class="gp">In [227]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a in b&apos;</span><span class="p">)</span>
<span class="gr">Out[227]: </span>
<span class="go">   a  b  c  d</span>
<span class="go">0  a  a  2  6</span>
<span class="go">1  a  a  4  7</span>
<span class="go">2  b  a  1  6</span>
<span class="go">3  b  a  2  1</span>
<span class="go">4  c  b  3  6</span>
<span class="go">5  c  b  0  2</span>

<span class="c"># How you&apos;d do it in pure Python</span>
<span class="gp">In [228]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">)]</span>
<span class="gr">Out[228]: </span>
<span class="go">   a  b  c  d</span>
<span class="go">0  a  a  2  6</span>
<span class="go">1  a  a  4  7</span>
<span class="go">2  b  a  1  6</span>
<span class="go">3  b  a  2  1</span>
<span class="go">4  c  b  3  6</span>
<span class="go">5  c  b  0  2</span>

<span class="gp">In [229]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a not in b&apos;</span><span class="p">)</span>
<span class="gr">Out[229]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">6   d  b  3  3</span>
<span class="go">7   d  b  2  1</span>
<span class="go">8   e  c  4  3</span>
<span class="go">9   e  c  2  0</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>

<span class="c"># pure Python</span>
<span class="gp">In [230]: </span><span class="n">df</span><span class="p">[</span><span class="o">~</span><span class="n">df</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">)]</span>
<span class="gr">Out[230]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">6   d  b  3  3</span>
<span class="go">7   d  b  2  1</span>
<span class="go">8   e  c  4  3</span>
<span class="go">9   e  c  2  0</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-363">您可以将其与其他表达式结合使用，以获得非常简洁的查询：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># rows where cols a and b have overlapping values and col c&apos;s values are less than col d&apos;s</span>
<span class="gp">In [231]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a in b and c &lt; d&apos;</span><span class="p">)</span>
<span class="gr">Out[231]: </span>
<span class="go">   a  b  c  d</span>
<span class="go">0  a  a  2  6</span>
<span class="go">1  a  a  4  7</span>
<span class="go">2  b  a  1  6</span>
<span class="go">4  c  b  3  6</span>
<span class="go">5  c  b  0  2</span>

<span class="c"># pure Python</span>
<span class="gp">In [232]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="o">.</span><span class="n">isin</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">a</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">c</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">d</span><span class="p">)]</span>
<span class="gr">Out[232]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">1   a  a  4  7</span>
<span class="go">2   b  a  1  6</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-364">注意</span></p>
<p><span class="yiyi-st" id="yiyi-365">Note that <code class="docutils literal"><span class="pre">in</span></code> and <code class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></code> are evaluated in Python, since <code class="docutils literal"><span class="pre">numexpr</span></code> has no equivalent of this operation. </span><span class="yiyi-st" id="yiyi-366">However, <strong>only the</strong> <code class="docutils literal"><span class="pre">in</span></code>/<code class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></code> <strong>expression itself</strong> is evaluated in vanilla Python. </span><span class="yiyi-st" id="yiyi-367">例如，在表达式中</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a in b + c + d&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p class="last"><span class="yiyi-st" id="yiyi-368"><code class="docutils literal"><span class="pre">（b</span> <span class="pre">+</span> <span class="pre">c</span> <span class="pre">+</span> <span class="pre">d）</span> 在普通Python中评估<code class="docutils literal"><span class="pre">numexpr</span></code>和<em>然后</em>中的<code class="docutils literal"><span class="pre">in</span></code></code></span><span class="yiyi-st" id="yiyi-369">通常，可以使用<code class="docutils literal"><span class="pre">numexpr</span></code>计算的任何操作都将是。</span></p>
</div>
</div>
<div class="section" id="special-use-of-the-operator-with-list-objects">
<h3><span class="yiyi-st" id="yiyi-370">Special use of the <code class="docutils literal"><span class="pre">==</span></code> operator with <code class="docutils literal"><span class="pre">list</span></code> objects</span></h3>
<p><span class="yiyi-st" id="yiyi-371">使用<code class="docutils literal"><span class="pre">==</span></code> / <code class="docutils literal"><span class="pre">!=</span></code>将<code class="docutils literal"><span class="pre">list</span></code>与<code class="docutils literal"><span class="pre">in</span></code> / <code class="docutils literal"><span class="pre">不是</span> <span class="pre">在</span></code>中</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [233]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;b == [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]&apos;</span><span class="p">)</span>
<span class="gr">Out[233]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">1   a  a  4  7</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">6   d  b  3  3</span>
<span class="go">7   d  b  2  1</span>
<span class="go">8   e  c  4  3</span>
<span class="go">9   e  c  2  0</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>

<span class="c"># pure Python</span>
<span class="gp">In [234]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">])]</span>
<span class="gr">Out[234]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">1   a  a  4  7</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">6   d  b  3  3</span>
<span class="go">7   d  b  2  1</span>
<span class="go">8   e  c  4  3</span>
<span class="go">9   e  c  2  0</span>
<span class="go">10  f  c  0  6</span>
<span class="go">11  f  c  1  2</span>

<span class="gp">In [235]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;c == [1, 2]&apos;</span><span class="p">)</span>
<span class="gr">Out[235]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">7   d  b  2  1</span>
<span class="go">9   e  c  2  0</span>
<span class="go">11  f  c  1  2</span>

<span class="gp">In [236]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;c != [1, 2]&apos;</span><span class="p">)</span>
<span class="gr">Out[236]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">1   a  a  4  7</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">6   d  b  3  3</span>
<span class="go">8   e  c  4  3</span>
<span class="go">10  f  c  0  6</span>

<span class="c"># using in/not in</span>
<span class="gp">In [237]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;[1, 2] in c&apos;</span><span class="p">)</span>
<span class="gr">Out[237]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">7   d  b  2  1</span>
<span class="go">9   e  c  2  0</span>
<span class="go">11  f  c  1  2</span>

<span class="gp">In [238]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;[1, 2] not in c&apos;</span><span class="p">)</span>
<span class="gr">Out[238]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">1   a  a  4  7</span>
<span class="go">4   c  b  3  6</span>
<span class="go">5   c  b  0  2</span>
<span class="go">6   d  b  3  3</span>
<span class="go">8   e  c  4  3</span>
<span class="go">10  f  c  0  6</span>

<span class="c"># pure Python</span>
<span class="gp">In [239]: </span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])]</span>
<span class="gr">Out[239]: </span>
<span class="go">    a  b  c  d</span>
<span class="go">0   a  a  2  6</span>
<span class="go">2   b  a  1  6</span>
<span class="go">3   b  a  2  1</span>
<span class="go">7   d  b  2  1</span>
<span class="go">9   e  c  2  0</span>
<span class="go">11  f  c  1  2</span>
</pre></div>
</div>
</div>
<div class="section" id="boolean-operators">
<h3><span class="yiyi-st" id="yiyi-372">Boolean Operators</span></h3>
<p><span class="yiyi-st" id="yiyi-373">您可以使用字<code class="docutils literal"><span class="pre">not</span></code>或<code class="docutils literal"><span class="pre">~</span></code>运算符来否定布尔表达式。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [240]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">))</span>

<span class="gp">In [241]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;bools&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">df</span><span class="p">))</span> <span class="o">&gt;</span> <span class="mf">0.5</span>

<span class="gp">In [242]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;~bools&apos;</span><span class="p">)</span>
<span class="gr">Out[242]: </span>
<span class="go">          a         b         c  bools</span>
<span class="go">2  0.697753  0.212799  0.329209  False</span>
<span class="go">7  0.275396  0.691034  0.826619  False</span>
<span class="go">8  0.190649  0.558748  0.262467  False</span>

<span class="gp">In [243]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;not bools&apos;</span><span class="p">)</span>
<span class="gr">Out[243]: </span>
<span class="go">          a         b         c  bools</span>
<span class="go">2  0.697753  0.212799  0.329209  False</span>
<span class="go">7  0.275396  0.691034  0.826619  False</span>
<span class="go">8  0.190649  0.558748  0.262467  False</span>

<span class="gp">In [244]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;not bools&apos;</span><span class="p">)</span> <span class="o">==</span> <span class="n">df</span><span class="p">[</span><span class="o">~</span><span class="n">df</span><span class="o">.</span><span class="n">bools</span><span class="p">]</span>
<span class="gr">Out[244]: </span>
<span class="go">      a     b     c bools</span>
<span class="go">2  True  True  True  True</span>
<span class="go">7  True  True  True  True</span>
<span class="go">8  True  True  True  True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-374">当然，表达式也可以是任意复杂的</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># short query syntax</span>
<span class="gp">In [245]: </span><span class="n">shorter</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;a &lt; b &lt; c and (not bools) or bools &gt; 2&apos;</span><span class="p">)</span>

<span class="c"># equivalent in pure Python</span>
<span class="gp">In [246]: </span><span class="n">longer</span> <span class="o">=</span> <span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">a</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">b</span> <span class="o">&lt;</span> <span class="n">df</span><span class="o">.</span><span class="n">c</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="o">~</span><span class="n">df</span><span class="o">.</span><span class="n">bools</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">bools</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">)]</span>

<span class="gp">In [247]: </span><span class="n">shorter</span>
<span class="gr">Out[247]: </span>
<span class="go">          a         b         c  bools</span>
<span class="go">7  0.275396  0.691034  0.826619  False</span>

<span class="gp">In [248]: </span><span class="n">longer</span>
<span class="gr">Out[248]: </span>
<span class="go">          a         b         c  bools</span>
<span class="go">7  0.275396  0.691034  0.826619  False</span>

<span class="gp">In [249]: </span><span class="n">shorter</span> <span class="o">==</span> <span class="n">longer</span>
<span class="gr">Out[249]: </span>
<span class="go">      a     b     c bools</span>
<span class="go">7  True  True  True  True</span>
</pre></div>
</div>
</div>
<div class="section" id="performance-of-query">
<h3><span class="yiyi-st" id="yiyi-375">Performance of <a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-376">对于大数据，<code class="docutils literal"><span class="pre">DataFrame.query()</span></code>使用<code class="docutils literal"><span class="pre">numexpr</span></code>稍快于Python</span></p>
<img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/query-perf.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/query-perf.png">
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-377">注意</span></p>
<p><span class="yiyi-st" id="yiyi-378">如果您的框架超过大约20万行，您将只能看到使用<code class="docutils literal"><span class="pre">numexpr</span></code>引擎和<code class="docutils literal"><span class="pre">DataFrame.query()</span></code>的性能优势</span></p>
<blockquote class="last">
<div><img alt="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/query-perf-small.png" src="http://pandas.pydata.org/pandas-docs/version/0.19.2/_images/query-perf-small.png">
</div></blockquote>
</div>
<p><span class="yiyi-st" id="yiyi-379">此图使用<code class="docutils literal"><span class="pre">DataFrame</span></code>创建，每个列包含使用<code class="docutils literal"><span class="pre">numpy.random.randn()</span></code>生成的浮点值。</span></p>
</div>
</div>
<div class="section" id="duplicate-data">
<h2><span class="yiyi-st" id="yiyi-380">Duplicate Data</span></h2>
<p id="indexing-duplicate"><span class="yiyi-st" id="yiyi-381">如果要在DataFrame中标识和删除重复行，有两种方法将有助于：<code class="docutils literal"><span class="pre">duplicated</span></code>和<code class="docutils literal"><span class="pre">drop_duplicates</span></code>。</span><span class="yiyi-st" id="yiyi-382">每个都将用作标识重复行的列作为参数。</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-383"><code class="docutils literal"><span class="pre">duplicated</span></code>返回布尔向量，其长度为行数，并指示行是否重复。</span></li>
<li><span class="yiyi-st" id="yiyi-384"><code class="docutils literal"><span class="pre">drop_duplicates</span></code>删除重复的行。</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-385">默认情况下，重复集的第一个观察到的行被认为是唯一的，但每个方法都有一个<code class="docutils literal"><span class="pre">keep</span></code>参数来指定要保留的目标。</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-386"><code class="docutils literal"><span class="pre">keep=&apos;first&apos;</span></code>（默认）：除第一次出现之外，标记/删除重复项。</span></li>
<li><span class="yiyi-st" id="yiyi-387"><code class="docutils literal"><span class="pre">keep=&apos;last&apos;</span></code>：标记/删除除最后一次出现的副本。</span></li>
<li><span class="yiyi-st" id="yiyi-388"><code class="docutils literal"><span class="pre">keep=False</span></code>：标记/删除所有重复项。</span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [250]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;three&apos;</span><span class="p">,</span> <span class="s1">&apos;four&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;y&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;y&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="s1">&apos;x&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">7</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [251]: </span><span class="n">df2</span>
<span class="gr">Out[251]: </span>
<span class="go">       a  b         c</span>
<span class="go">0    one  x -1.067137</span>
<span class="go">1    one  y  0.309500</span>
<span class="go">2    two  x -0.211056</span>
<span class="go">3    two  y -1.842023</span>
<span class="go">4    two  x -0.390820</span>
<span class="go">5  three  x -1.964475</span>
<span class="go">6   four  x  1.298329</span>

<span class="gp">In [252]: </span><span class="n">df2</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">)</span>
<span class="gr">Out[252]: </span>
<span class="go">0    False</span>
<span class="go">1     True</span>
<span class="go">2    False</span>
<span class="go">3     True</span>
<span class="go">4     True</span>
<span class="go">5    False</span>
<span class="go">6    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [253]: </span><span class="n">df2</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="n">keep</span><span class="o">=</span><span class="s1">&apos;last&apos;</span><span class="p">)</span>
<span class="gr">Out[253]: </span>
<span class="go">0     True</span>
<span class="go">1    False</span>
<span class="go">2     True</span>
<span class="go">3     True</span>
<span class="go">4    False</span>
<span class="go">5    False</span>
<span class="go">6    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [254]: </span><span class="n">df2</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="n">keep</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[254]: </span>
<span class="go">0     True</span>
<span class="go">1     True</span>
<span class="go">2     True</span>
<span class="go">3     True</span>
<span class="go">4     True</span>
<span class="go">5    False</span>
<span class="go">6    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [255]: </span><span class="n">df2</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">)</span>
<span class="gr">Out[255]: </span>
<span class="go">       a  b         c</span>
<span class="go">0    one  x -1.067137</span>
<span class="go">2    two  x -0.211056</span>
<span class="go">5  three  x -1.964475</span>
<span class="go">6   four  x  1.298329</span>

<span class="gp">In [256]: </span><span class="n">df2</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="n">keep</span><span class="o">=</span><span class="s1">&apos;last&apos;</span><span class="p">)</span>
<span class="gr">Out[256]: </span>
<span class="go">       a  b         c</span>
<span class="go">1    one  y  0.309500</span>
<span class="go">4    two  x -0.390820</span>
<span class="go">5  three  x -1.964475</span>
<span class="go">6   four  x  1.298329</span>

<span class="gp">In [257]: </span><span class="n">df2</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="n">keep</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[257]: </span>
<span class="go">       a  b         c</span>
<span class="go">5  three  x -1.964475</span>
<span class="go">6   four  x  1.298329</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-389">此外，您可以传递列的列表以识别重复。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [258]: </span><span class="n">df2</span><span class="o">.</span><span class="n">duplicated</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">])</span>
<span class="gr">Out[258]: </span>
<span class="go">0    False</span>
<span class="go">1    False</span>
<span class="go">2    False</span>
<span class="go">3    False</span>
<span class="go">4     True</span>
<span class="go">5    False</span>
<span class="go">6    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [259]: </span><span class="n">df2</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">])</span>
<span class="gr">Out[259]: </span>
<span class="go">       a  b         c</span>
<span class="go">0    one  x -1.067137</span>
<span class="go">1    one  y  0.309500</span>
<span class="go">2    two  x -0.211056</span>
<span class="go">3    two  y -1.842023</span>
<span class="go">5  three  x -1.964475</span>
<span class="go">6   four  x  1.298329</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-390">要按索引值删除重复项，请使用<code class="docutils literal"><span class="pre">Index.duplicated</span></code>，然后执行切片。</span><span class="yiyi-st" id="yiyi-391">相同的选项在<code class="docutils literal"><span class="pre">keep</span></code>参数中可用。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [260]: </span><span class="n">df3</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">),</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">6</span><span class="p">)},</span>
<span class="gp">   .....:</span>                    <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [261]: </span><span class="n">df3</span>
<span class="gr">Out[261]: </span>
<span class="go">   a         b</span>
<span class="go">a  0  1.440455</span>
<span class="go">a  1  2.456086</span>
<span class="go">b  2  1.038402</span>
<span class="go">c  3 -0.894409</span>
<span class="go">b  4  0.683536</span>
<span class="go">a  5  3.082764</span>

<span class="gp">In [262]: </span><span class="n">df3</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">duplicated</span><span class="p">()</span>
<span class="gr">Out[262]: </span><span class="n">array</span><span class="p">([</span><span class="bp">False</span><span class="p">,</span>  <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span>  <span class="bp">True</span><span class="p">,</span>  <span class="bp">True</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>

<span class="gp">In [263]: </span><span class="n">df3</span><span class="p">[</span><span class="o">~</span><span class="n">df3</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">duplicated</span><span class="p">()]</span>
<span class="gr">Out[263]: </span>
<span class="go">   a         b</span>
<span class="go">a  0  1.440455</span>
<span class="go">b  2  1.038402</span>
<span class="go">c  3 -0.894409</span>

<span class="gp">In [264]: </span><span class="n">df3</span><span class="p">[</span><span class="o">~</span><span class="n">df3</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="n">keep</span><span class="o">=</span><span class="s1">&apos;last&apos;</span><span class="p">)]</span>
<span class="gr">Out[264]: </span>
<span class="go">   a         b</span>
<span class="go">c  3 -0.894409</span>
<span class="go">b  4  0.683536</span>
<span class="go">a  5  3.082764</span>

<span class="gp">In [265]: </span><span class="n">df3</span><span class="p">[</span><span class="o">~</span><span class="n">df3</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">duplicated</span><span class="p">(</span><span class="n">keep</span><span class="o">=</span><span class="bp">False</span><span class="p">)]</span>
<span class="gr">Out[265]: </span>
<span class="go">   a         b</span>
<span class="go">c  3 -0.894409</span>
</pre></div>
</div>
</div>
<div class="section" id="dictionary-like-get-method">
<span id="indexing-dictionarylike"></span><h2><span class="yiyi-st" id="yiyi-392">Dictionary-like <a class="reference internal" href="generated/pandas.DataFrame.get.html#pandas.DataFrame.get" title="pandas.DataFrame.get"><code class="xref py py-meth docutils literal"><span class="pre">get()</span></code></a> method</span></h2>
<p><span class="yiyi-st" id="yiyi-393">Series，DataFrame和Panel中的每一个都有一个<code class="docutils literal"><span class="pre">get</span></code>方法，可以返回默认值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [266]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">,</span><span class="s1">&apos;c&apos;</span><span class="p">])</span>

<span class="gp">In [267]: </span><span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">)</span>               <span class="c1"># equivalent to s[&apos;a&apos;]</span>
<span class="gr">Out[267]: </span><span class="mi">1</span>

<span class="gp">In [268]: </span><span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&apos;x&apos;</span><span class="p">,</span> <span class="n">default</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[268]: </span><span class="o">-</span><span class="mi">1</span>
</pre></div>
</div>
</div>
<div class="section" id="the-select-method">
<h2><span class="yiyi-st" id="yiyi-394">The <a class="reference internal" href="generated/pandas.DataFrame.select.html#pandas.DataFrame.select" title="pandas.DataFrame.select"><code class="xref py py-meth docutils literal"><span class="pre">select()</span></code></a> Method</span></h2>
<p><span class="yiyi-st" id="yiyi-395">从对象中提取切片的另一种方法是使用Series，DataFrame和Panel的<code class="docutils literal"><span class="pre">select</span></code>方法。</span><span class="yiyi-st" id="yiyi-396">这个方法应该只在没有更多的直接方式时使用。</span><span class="yiyi-st" id="yiyi-397"><code class="docutils literal"><span class="pre">select</span></code>采用对<code class="docutils literal"><span class="pre">axis</span></code>的标签进行操作的函数，并返回布尔值。</span><span class="yiyi-st" id="yiyi-398">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [269]: </span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">==</span> <span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[269]: </span>
<span class="go">                   A</span>
<span class="go">2000-01-01  0.355794</span>
<span class="go">2000-01-02  1.635763</span>
<span class="go">2000-01-03  0.854409</span>
<span class="go">2000-01-04 -0.216659</span>
<span class="go">2000-01-05  2.414688</span>
<span class="go">2000-01-06 -1.206215</span>
<span class="go">2000-01-07  0.779461</span>
<span class="go">2000-01-08 -0.878999</span>
</pre></div>
</div>
</div>
<div class="section" id="the-lookup-method">
<h2><span class="yiyi-st" id="yiyi-399">The <a class="reference internal" href="generated/pandas.DataFrame.lookup.html#pandas.DataFrame.lookup" title="pandas.DataFrame.lookup"><code class="xref py py-meth docutils literal"><span class="pre">lookup()</span></code></a> Method</span></h2>
<p><span class="yiyi-st" id="yiyi-400">有时，您希望提取一组给定一系列行标签和列标签的值，而<code class="docutils literal"><span class="pre">lookup</span></code>方法允许此操作并返回numpy数组。</span><span class="yiyi-st" id="yiyi-401">例如，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [270]: </span><span class="n">dflookup</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">,</span><span class="s1">&apos;D&apos;</span><span class="p">])</span>

<span class="gp">In [271]: </span><span class="n">dflookup</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)),</span> <span class="p">[</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;D&apos;</span><span class="p">])</span>
<span class="gr">Out[271]: </span><span class="n">array</span><span class="p">([</span> <span class="mf">0.3506</span><span class="p">,</span>  <span class="mf">0.4779</span><span class="p">,</span>  <span class="mf">0.4825</span><span class="p">,</span>  <span class="mf">0.9197</span><span class="p">,</span>  <span class="mf">0.5019</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="index-objects">
<span id="indexing-class"></span><h2><span class="yiyi-st" id="yiyi-402">Index objects</span></h2>
<p><span class="yiyi-st" id="yiyi-403">pandas <a class="reference internal" href="generated/pandas.Index.html#pandas.Index" title="pandas.Index"><code class="xref py py-class docutils literal"><span class="pre">Index</span></code></a>类及其子类可以被视为实现<em>有序多集</em>。</span><span class="yiyi-st" id="yiyi-404">允许重复。</span><span class="yiyi-st" id="yiyi-405">但是，如果您尝试将具有重复条目的<a class="reference internal" href="generated/pandas.Index.html#pandas.Index" title="pandas.Index"><code class="xref py py-class docutils literal"><span class="pre">Index</span></code></a>对象转换为<code class="docutils literal"><span class="pre">set</span></code>，则会引发异常。</span></p>
<p><span class="yiyi-st" id="yiyi-406"><a class="reference internal" href="generated/pandas.Index.html#pandas.Index" title="pandas.Index"><code class="xref py py-class docutils literal"><span class="pre">Index</span></code></a>还提供了查找，数据对齐和重建索引所需的基础结构。</span><span class="yiyi-st" id="yiyi-407">要直接创建<a class="reference internal" href="generated/pandas.Index.html#pandas.Index" title="pandas.Index"><code class="xref py py-class docutils literal"><span class="pre">Index</span></code></a>的最简单方法是将<code class="docutils literal"><span class="pre">list</span></code>或其他序列传递给<a class="reference internal" href="generated/pandas.Index.html#pandas.Index" title="pandas.Index"><code class="xref py py-class docutils literal"><span class="pre">Index</span></code></a>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [272]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">])</span>

<span class="gp">In [273]: </span><span class="n">index</span>
<span class="gr">Out[273]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;e&apos;</span><span class="p">,</span> <span class="s1">u&apos;d&apos;</span><span class="p">,</span> <span class="s1">u&apos;a&apos;</span><span class="p">,</span> <span class="s1">u&apos;b&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [274]: </span><span class="s1">&apos;d&apos;</span> <span class="ow">in</span> <span class="n">index</span>
<span class="gr">Out[274]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-408">您还可以传递<code class="docutils literal"><span class="pre">name</span></code>以存储在索引中：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [275]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s1">&apos;something&apos;</span><span class="p">)</span>

<span class="gp">In [276]: </span><span class="n">index</span><span class="o">.</span><span class="n">name</span>
<span class="gr">Out[276]: </span><span class="s1">&apos;something&apos;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-409">名称（如果设置）将显示在控制台显示中：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [277]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&apos;rows&apos;</span><span class="p">)</span>

<span class="gp">In [278]: </span><span class="n">columns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s1">&apos;cols&apos;</span><span class="p">)</span>

<span class="gp">In [279]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>

<span class="gp">In [280]: </span><span class="n">df</span>
<span class="gr">Out[280]: </span>
<span class="go">cols         A         B         C</span>
<span class="go">rows                              </span>
<span class="go">0     1.295989  0.185778  0.436259</span>
<span class="go">1     0.678101  0.311369 -0.528378</span>
<span class="go">2    -0.674808 -1.103529 -0.656157</span>
<span class="go">3     1.889957  2.076651 -1.102192</span>
<span class="go">4    -1.211795 -0.791746  0.634724</span>

<span class="gp">In [281]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span>
<span class="gr">Out[281]: </span>
<span class="go">rows</span>
<span class="go">0    1.295989</span>
<span class="go">1    0.678101</span>
<span class="go">2   -0.674808</span>
<span class="go">3    1.889957</span>
<span class="go">4   -1.211795</span>
<span class="go">Name: A, dtype: float64</span>
</pre></div>
</div>
<div class="section" id="setting-metadata">
<span id="indexing-set-metadata"></span><h3><span class="yiyi-st" id="yiyi-410">Setting metadata</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-411"><span class="versionmodified">版本0.13.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-412">Indexes are “mostly immutable”, but it is possible to set and change their metadata, like the index <code class="docutils literal"><span class="pre">name</span></code> (or, for <code class="docutils literal"><span class="pre">MultiIndex</span></code>, <code class="docutils literal"><span class="pre">levels</span></code> and <code class="docutils literal"><span class="pre">labels</span></code>).</span></p>
<p><span class="yiyi-st" id="yiyi-413">您可以使用<code class="docutils literal"><span class="pre">rename</span></code>，<code class="docutils literal"><span class="pre">set_names</span></code>，<code class="docutils literal"><span class="pre">set_levels</span></code>和<code class="docutils literal"><span class="pre">set_labels</span></code>直接设置这些属性。</span><span class="yiyi-st" id="yiyi-414">他们默认返回一份副本；但是，您可以指定<code class="docutils literal"><span class="pre">inplace=True</span></code>以使数据更改到位。</span></p>
<p><span class="yiyi-st" id="yiyi-415">有关MultiIndexes的用法，请参见<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">Advanced Indexing</span></a>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [282]: </span><span class="n">ind</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>

<span class="gp">In [283]: </span><span class="n">ind</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="s2">&quot;apple&quot;</span><span class="p">)</span>
<span class="gr">Out[283]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">u&apos;apple&apos;</span><span class="p">)</span>

<span class="gp">In [284]: </span><span class="n">ind</span>
<span class="gr">Out[284]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [285]: </span><span class="n">ind</span><span class="o">.</span><span class="n">set_names</span><span class="p">([</span><span class="s2">&quot;apple&quot;</span><span class="p">],</span> <span class="n">inplace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [286]: </span><span class="n">ind</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;bob&quot;</span>

<span class="gp">In [287]: </span><span class="n">ind</span>
<span class="gr">Out[287]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">u&apos;bob&apos;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-416"><span class="versionmodified">版本0.15.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-417"><code class="docutils literal"><span class="pre">set_names</span></code>，<code class="docutils literal"><span class="pre">set_levels</span></code>和<code class="docutils literal"><span class="pre">set_labels</span></code>也采用可选的<cite>级别</cite>参数</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [288]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">]],</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;first&apos;</span><span class="p">,</span> <span class="s1">&apos;second&apos;</span><span class="p">])</span>

<span class="gp">In [289]: </span><span class="n">index</span>
<span class="gr">Out[289]: </span>
<span class="go">MultiIndex(levels=[[0, 1, 2], [u&apos;one&apos;, u&apos;two&apos;]],</span>
<span class="go">           labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],</span>
<span class="go">           names=[u&apos;first&apos;, u&apos;second&apos;])</span>

<span class="gp">In [290]: </span><span class="n">index</span><span class="o">.</span><span class="n">levels</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[290]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;one&apos;</span><span class="p">,</span> <span class="s1">u&apos;two&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">u&apos;second&apos;</span><span class="p">)</span>

<span class="gp">In [291]: </span><span class="n">index</span><span class="o">.</span><span class="n">set_levels</span><span class="p">([</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">],</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[291]: </span>
<span class="go">MultiIndex(levels=[[0, 1, 2], [u&apos;a&apos;, u&apos;b&apos;]],</span>
<span class="go">           labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],</span>
<span class="go">           names=[u&apos;first&apos;, u&apos;second&apos;])</span>
</pre></div>
</div>
</div>
<div class="section" id="set-operations-on-index-objects">
<h3><span class="yiyi-st" id="yiyi-418">Set operations on Index objects</span></h3>
<div class="admonition warning" id="indexing-set-ops">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-419">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-420">在0.15.0. 不建议使用set操作<code class="docutils literal"><span class="pre">+</span></code>和<code class="docutils literal"><span class="pre">-</span></code>，以便为某些索引类型的数字类型操作提供这些操作。</span><span class="yiyi-st" id="yiyi-421"><code class="docutils literal"><span class="pre">+</span></code> can be replace by <code class="docutils literal"><span class="pre">.union()</span></code> or <code class="docutils literal"><span class="pre">|</span></code>, and <code class="docutils literal"><span class="pre">-</span></code> by <code class="docutils literal"><span class="pre">.difference()</span></code>.</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-422">两个主要操作是<code class="docutils literal"><span class="pre">联合</span> <span class="pre">（|）</span></code>，<code class="docutils literal"><span class="pre">交集</span> <span class="pre">（＆amp； / t5&gt;</span></code>这些可以直接调用为实例方法或通过重载运算符使用。</span><span class="yiyi-st" id="yiyi-423">差异通过<code class="docutils literal"><span class="pre">.difference()</span></code>方法提供。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [292]: </span><span class="n">a</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">])</span>

<span class="gp">In [293]: </span><span class="n">b</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">])</span>

<span class="gp">In [294]: </span><span class="n">a</span> <span class="o">|</span> <span class="n">b</span>
<span class="gr">Out[294]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;a&apos;</span><span class="p">,</span> <span class="s1">u&apos;b&apos;</span><span class="p">,</span> <span class="s1">u&apos;c&apos;</span><span class="p">,</span> <span class="s1">u&apos;d&apos;</span><span class="p">,</span> <span class="s1">u&apos;e&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [295]: </span><span class="n">a</span> <span class="o">&amp;</span> <span class="n">b</span>
<span class="gr">Out[295]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;c&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>

<span class="gp">In [296]: </span><span class="n">a</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="gr">Out[296]: </span><span class="n">Index</span><span class="p">([</span><span class="s1">u&apos;a&apos;</span><span class="p">,</span> <span class="s1">u&apos;b&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;object&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-424">还可以使用<code class="docutils literal"><span class="pre">symmetric_difference</span> <span class="pre">（^）</span></code>操作，返回出现在<code class="docutils literal"><span class="pre">idx1</span></code>或<code class="docutils literal"><span class="pre">idx2</span></code>，但不能同时包含两者。</span><span class="yiyi-st" id="yiyi-425">这相当于由<code class="docutils literal"><span class="pre">idx1.difference(idx2).union(idx2.difference(idx1))</span></code>创建的索引，重复项被删除。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [297]: </span><span class="n">idx1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>

<span class="gp">In [298]: </span><span class="n">idx2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>

<span class="gp">In [299]: </span><span class="n">idx1</span><span class="o">.</span><span class="n">symmetric_difference</span><span class="p">(</span><span class="n">idx2</span><span class="p">)</span>
<span class="gr">Out[299]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [300]: </span><span class="n">idx1</span> <span class="o">^</span> <span class="n">idx2</span>
<span class="gr">Out[300]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="missing-values">
<h3><span class="yiyi-st" id="yiyi-426">Missing values</span></h3>
<div class="versionadded" id="indexing-missing">
<p><span class="yiyi-st" id="yiyi-427"><span class="versionmodified">版本0.17.1中的新功能。</span></span></p>
</div>
<div class="admonition important">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-428">重要</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-429">即使<code class="docutils literal"><span class="pre">Index</span></code>可以保存缺失值（<code class="docutils literal"><span class="pre">NaN</span></code>），如果不想要任何意外结果，应该避免。</span><span class="yiyi-st" id="yiyi-430">例如，某些操作会隐式地排除缺失值。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-431"><code class="docutils literal"><span class="pre">Index.fillna</span></code>使用指定的标量值填充缺失值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [301]: </span><span class="n">idx1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>

<span class="gp">In [302]: </span><span class="n">idx1</span>
<span class="gr">Out[302]: </span><span class="n">Float64Index</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">nan</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;float64&apos;</span><span class="p">)</span>

<span class="gp">In [303]: </span><span class="n">idx1</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[303]: </span><span class="n">Float64Index</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;float64&apos;</span><span class="p">)</span>

<span class="gp">In [304]: </span><span class="n">idx2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">),</span> <span class="n">pd</span><span class="o">.</span><span class="n">NaT</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2011-01-03&apos;</span><span class="p">)])</span>

<span class="gp">In [305]: </span><span class="n">idx2</span>
<span class="gr">Out[305]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;NaT&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-01-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [306]: </span><span class="n">idx2</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;2011-01-02&apos;</span><span class="p">))</span>
<span class="gr">Out[306]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2011-01-01&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-01-02&apos;</span><span class="p">,</span> <span class="s1">&apos;2011-01-03&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="set-reset-index">
<h2><span class="yiyi-st" id="yiyi-432">Set / Reset Index</span></h2>
<p><span class="yiyi-st" id="yiyi-433">有时，您将在DataFrame中加载或创建数据集，并且希望在已经这样做之后添加索引。</span><span class="yiyi-st" id="yiyi-434">有两种不同的方法。</span></p>
<div class="section" id="set-an-index">
<h3><span class="yiyi-st" id="yiyi-435">Set an index</span></h3>
<p id="indexing-set-index"><span class="yiyi-st" id="yiyi-436">DataFrame有一个<code class="docutils literal"><span class="pre">set_index</span></code>方法，它使用列名（对于常规<code class="docutils literal"><span class="pre">Index</span></code>）或列名列表（对于<code class="docutils literal"><span class="pre">MultiIndex</span></code>），创建一个新的，索引的DataFrame：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [307]: </span><span class="n">data</span>
<span class="gr">Out[307]: </span>
<span class="go">     a    b  c    d</span>
<span class="go">0  bar  one  z  1.0</span>
<span class="go">1  bar  two  y  2.0</span>
<span class="go">2  foo  one  x  3.0</span>
<span class="go">3  foo  two  w  4.0</span>

<span class="gp">In [308]: </span><span class="n">indexed1</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&apos;c&apos;</span><span class="p">)</span>

<span class="gp">In [309]: </span><span class="n">indexed1</span>
<span class="gr">Out[309]: </span>
<span class="go">     a    b    d</span>
<span class="go">c               </span>
<span class="go">z  bar  one  1.0</span>
<span class="go">y  bar  two  2.0</span>
<span class="go">x  foo  one  3.0</span>
<span class="go">w  foo  two  4.0</span>

<span class="gp">In [310]: </span><span class="n">indexed2</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">])</span>

<span class="gp">In [311]: </span><span class="n">indexed2</span>
<span class="gr">Out[311]: </span>
<span class="go">         c    d</span>
<span class="go">a   b          </span>
<span class="go">bar one  z  1.0</span>
<span class="go">    two  y  2.0</span>
<span class="go">foo one  x  3.0</span>
<span class="go">    two  w  4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-437"><code class="docutils literal"><span class="pre">append</span></code>关键字选项允许您保留现有索引并将给定列附加到MultiIndex：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [312]: </span><span class="n">frame</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

<span class="gp">In [313]: </span><span class="n">frame</span> <span class="o">=</span> <span class="n">frame</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="n">append</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [314]: </span><span class="n">frame</span>
<span class="gr">Out[314]: </span>
<span class="go">           c    d</span>
<span class="go">c a   b          </span>
<span class="go">z bar one  z  1.0</span>
<span class="go">y bar two  y  2.0</span>
<span class="go">x foo one  x  3.0</span>
<span class="go">w foo two  w  4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-438"><code class="docutils literal"><span class="pre">set_index</span></code>中的其他选项允许您不删除索引列或在现场添加索引（不创建新对象）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [315]: </span><span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="n">drop</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[315]: </span>
<span class="go">     a    b  c    d</span>
<span class="go">c                  </span>
<span class="go">z  bar  one  z  1.0</span>
<span class="go">y  bar  two  y  2.0</span>
<span class="go">x  foo  one  x  3.0</span>
<span class="go">w  foo  two  w  4.0</span>

<span class="gp">In [316]: </span><span class="n">data</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="n">inplace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="gp">In [317]: </span><span class="n">data</span>
<span class="gr">Out[317]: </span>
<span class="go">         c    d</span>
<span class="go">a   b          </span>
<span class="go">bar one  z  1.0</span>
<span class="go">    two  y  2.0</span>
<span class="go">foo one  x  3.0</span>
<span class="go">    two  w  4.0</span>
</pre></div>
</div>
</div>
<div class="section" id="reset-the-index">
<h3><span class="yiyi-st" id="yiyi-439">Reset the index</span></h3>
<p><span class="yiyi-st" id="yiyi-440">为方便起见，DataFrame上有一个称为<code class="docutils literal"><span class="pre">reset_index</span></code>的新函数，它将索引值传输到DataFrame的列中，并设置一个简单的整数索引。</span><span class="yiyi-st" id="yiyi-441">这是<code class="docutils literal"><span class="pre">set_index</span></code>的逆操作</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [318]: </span><span class="n">data</span>
<span class="gr">Out[318]: </span>
<span class="go">         c    d</span>
<span class="go">a   b          </span>
<span class="go">bar one  z  1.0</span>
<span class="go">    two  y  2.0</span>
<span class="go">foo one  x  3.0</span>
<span class="go">    two  w  4.0</span>

<span class="gp">In [319]: </span><span class="n">data</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
<span class="gr">Out[319]: </span>
<span class="go">     a    b  c    d</span>
<span class="go">0  bar  one  z  1.0</span>
<span class="go">1  bar  two  y  2.0</span>
<span class="go">2  foo  one  x  3.0</span>
<span class="go">3  foo  two  w  4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-442">输出与SQL表或记录数组更相似。</span><span class="yiyi-st" id="yiyi-443">从索引派生的列的名称是存储在<code class="docutils literal"><span class="pre">names</span></code>属性中的名称。</span></p>
<p><span class="yiyi-st" id="yiyi-444">您可以使用<code class="docutils literal"><span class="pre">level</span></code>关键字仅删除索引的一部分：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [320]: </span><span class="n">frame</span>
<span class="gr">Out[320]: </span>
<span class="go">           c    d</span>
<span class="go">c a   b          </span>
<span class="go">z bar one  z  1.0</span>
<span class="go">y bar two  y  2.0</span>
<span class="go">x foo one  x  3.0</span>
<span class="go">w foo two  w  4.0</span>

<span class="gp">In [321]: </span><span class="n">frame</span><span class="o">.</span><span class="n">reset_index</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[321]: </span>
<span class="go">         a  c    d</span>
<span class="go">c b               </span>
<span class="go">z one  bar  z  1.0</span>
<span class="go">y two  bar  y  2.0</span>
<span class="go">x one  foo  x  3.0</span>
<span class="go">w two  foo  w  4.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-445"><code class="docutils literal"><span class="pre">reset_index</span></code>使用一个可选参数<code class="docutils literal"><span class="pre">drop</span></code>，如果为true，只是丢弃索引，而不是在DataFrame的列中添加索引值。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-446">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-447"><code class="docutils literal"><span class="pre">reset_index</span></code>方法以前称为<code class="docutils literal"><span class="pre">delevel</span></code>，现在已被弃用。</span></p>
</div>
</div>
<div class="section" id="adding-an-ad-hoc-index">
<h3><span class="yiyi-st" id="yiyi-448">Adding an ad hoc index</span></h3>
<p><span class="yiyi-st" id="yiyi-449">如果您自己创建索引，则只需将其分配给<code class="docutils literal"><span class="pre">index</span></code>字段：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">index</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="returning-a-view-versus-a-copy">
<span id="indexing-view-versus-copy"></span><h2><span class="yiyi-st" id="yiyi-450">Returning a view versus a copy</span></h2>
<p><span class="yiyi-st" id="yiyi-451">在pandas对象中设置值时，必须注意避免使用<code class="docutils literal"><span class="pre">链接</span> <span class="pre">索引</span></code>。</span><span class="yiyi-st" id="yiyi-452">这里是一个例子。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [322]: </span><span class="n">dfmi</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abcd&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                      <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;efgh&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                      <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ijkl&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                      <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;mnop&apos;</span><span class="p">)],</span>
<span class="gp">   .....:</span>                     <span class="n">columns</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_product</span><span class="p">([[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span><span class="s1">&apos;two&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                                                         <span class="p">[</span><span class="s1">&apos;first&apos;</span><span class="p">,</span><span class="s1">&apos;second&apos;</span><span class="p">]]))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [323]: </span><span class="n">dfmi</span>
<span class="gr">Out[323]: </span>
<span class="go">    one          two       </span>
<span class="go">  first second first second</span>
<span class="go">0     a      b     c      d</span>
<span class="go">1     e      f     g      h</span>
<span class="go">2     i      j     k      l</span>
<span class="go">3     m      n     o      p</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-453">比较这两种访问方法：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [324]: </span><span class="n">dfmi</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">][</span><span class="s1">&apos;second&apos;</span><span class="p">]</span>
<span class="gr">Out[324]: </span>
<span class="go">0    b</span>
<span class="go">1    f</span>
<span class="go">2    j</span>
<span class="go">3    n</span>
<span class="go">Name: second, dtype: object</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [325]: </span><span class="n">dfmi</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,(</span><span class="s1">&apos;one&apos;</span><span class="p">,</span><span class="s1">&apos;second&apos;</span><span class="p">)]</span>
<span class="gr">Out[325]: </span>
<span class="go">0    b</span>
<span class="go">1    f</span>
<span class="go">2    j</span>
<span class="go">3    n</span>
<span class="go">Name: (one, second), dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-454">这两个都产生相同的结果，所以你应该使用？</span><span class="yiyi-st" id="yiyi-455">理解这些操作的顺序是有益的，为什么方法2（<code class="docutils literal"><span class="pre">.loc</span></code>）比方法1（链<code class="docutils literal"><span class="pre">[]</span></code></span></p>
<p><span class="yiyi-st" id="yiyi-456"><code class="docutils literal"><span class="pre">dfmi[&apos;one&apos;]</span></code>选择列的第一级，并返回单索引的DataFrame。</span><span class="yiyi-st" id="yiyi-457">然后，另一个python操作<code class="docutils literal"><span class="pre">dfmi_with_one[&apos;second&apos;]</span></code>选择由<code class="docutils literal"><span class="pre">&apos;second&apos;</span></code></span><span class="yiyi-st" id="yiyi-458">这由变量<code class="docutils literal"><span class="pre">dfmi_with_one</span></code>指示，因为pandas将这些操作视为单独的事件。</span><span class="yiyi-st" id="yiyi-459">例如单独调用<code class="docutils literal"><span class="pre">__getitem__</span></code>，因此它必须将它们视为线性操作，它们会一个接一个地发生。</span></p>
<p><span class="yiyi-st" id="yiyi-460">与<code class="docutils literal"><span class="pre">df.loc[:,(&apos;one&apos;,&apos;second&apos;)]</span></code>对比，其传递嵌套的元组<code class="docutils literal"><span class="pre">(slice(None),(&apos;one&apos;,&apos;second&apos;))</span></code>一次调用<code class="docutils literal"><span class="pre">__getitem__</span></code>。</span><span class="yiyi-st" id="yiyi-461">这允许熊猫作为一个单一的实体处理。</span><span class="yiyi-st" id="yiyi-462">此外，这种操作顺序<em>可以</em>明显更快，并且如果需要的话，允许索引<em>两个</em>轴。</span></p>
<div class="section" id="why-does-assignment-fail-when-using-chained-indexing">
<h3><span class="yiyi-st" id="yiyi-463">Why does assignment fail when using chained indexing?</span></h3>
<p><span class="yiyi-st" id="yiyi-464">上一节中的问题只是一个性能问题。</span><span class="yiyi-st" id="yiyi-465">什么是<code class="docutils literal"><span class="pre">SettingWithCopy</span></code>警告？</span><span class="yiyi-st" id="yiyi-466">我们不会<strong>通常</strong>在您执行可能需要花费更多毫秒的操作时抛出警告！</span></p>
<p><span class="yiyi-st" id="yiyi-467">但事实证明，对链接索引的产品分配具有固有的不可预测的结果。</span><span class="yiyi-st" id="yiyi-468">要看到这一点，想想Python解释器如何执行这段代码：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfmi</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,(</span><span class="s1">&apos;one&apos;</span><span class="p">,</span><span class="s1">&apos;second&apos;</span><span class="p">)]</span> <span class="o">=</span> <span class="n">value</span>
<span class="c1"># becomes</span>
<span class="n">dfmi</span><span class="o">.</span><span class="n">loc</span><span class="o">.</span><span class="n">__setitem__</span><span class="p">((</span><span class="nb">slice</span><span class="p">(</span><span class="bp">None</span><span class="p">),</span> <span class="p">(</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;second&apos;</span><span class="p">)),</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-469">但是这个代码处理方式不同：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfmi</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">][</span><span class="s1">&apos;second&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="c1"># becomes</span>
<span class="n">dfmi</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="s1">&apos;one&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">__setitem__</span><span class="p">(</span><span class="s1">&apos;second&apos;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-470">看到<code class="docutils literal"><span class="pre">__getitem__</span></code>在那里？</span><span class="yiyi-st" id="yiyi-471">Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which <em>pandas</em> makes no guarantees), and therefore whether the <code class="docutils literal"><span class="pre">__setitem__</span></code> will modify <code class="docutils literal"><span class="pre">dfmi</span></code> or a temporary object that gets thrown out immediately afterward. </span><span class="yiyi-st" id="yiyi-472"><strong>这是</strong>什么<code class="docutils literal"><span class="pre">SettingWithCopy</span></code>警告你！</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-473">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-474">您可能想知道我们是否应该关注第一个示例中的<code class="docutils literal"><span class="pre">loc</span></code>属性。</span><span class="yiyi-st" id="yiyi-475">但是<code class="docutils literal"><span class="pre">dfmi.loc</span></code>保证为<code class="docutils literal"><span class="pre">dfmi</span></code>本身具有修改的索引行为，因此<code class="docutils literal"><span class="pre">dfmi.loc.__getitem__</span></code> / <code class="docutils literal"><span class="pre">dfmi.loc.__setitem__</span></code>直接操作<code class="docutils literal"><span class="pre">dfmi</span></code>。</span><span class="yiyi-st" id="yiyi-476">当然，<code class="docutils literal"><span class="pre">dfmi.loc.__getitem__(idx)</span></code>可以是<code class="docutils literal"><span class="pre">dfmi</span></code>的视图或副本。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-477">有时，当没有明显的链接索引时，会出现<code class="docutils literal"><span class="pre">SettingWithCopy</span></code>警告。</span><span class="yiyi-st" id="yiyi-478"><strong>这些</strong>是<code class="docutils literal"><span class="pre">SettingWithCopy</span></code>旨在捕获的错误！</span><span class="yiyi-st" id="yiyi-479">熊猫可能试图警告你，你这样做：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">do_something</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
   <span class="n">foo</span> <span class="o">=</span> <span class="n">df</span><span class="p">[[</span><span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">]]</span>  <span class="c1"># Is foo a view? A copy? Nobody knows!</span>
   <span class="c1"># ... many lines here ...</span>
   <span class="n">foo</span><span class="p">[</span><span class="s1">&apos;quux&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>       <span class="c1"># We don&apos;t know whether this will modify df or not!</span>
   <span class="k">return</span> <span class="n">foo</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-480">Yikes！</span></p>
</div>
<div class="section" id="evaluation-order-matters">
<h3><span class="yiyi-st" id="yiyi-481">Evaluation order matters</span></h3>
<p><span class="yiyi-st" id="yiyi-482">此外，在链接表达式中，顺序可以确定是否返回副本。</span><span class="yiyi-st" id="yiyi-483">如果一个表达式在一个slice的副本上设置值，那么会出现一个<code class="docutils literal"><span class="pre">SettingWithCopy</span></code>异常（这个从0.13.0开始的raise / warn行为是新的）</span></p>
<p><span class="yiyi-st" id="yiyi-484">您可以通过选项<code class="docutils literal"><span class="pre">mode.chained_assignment</span></code>控制链接分配的操作，该选项可以取值<code class="docutils literal"><span class="pre">[&apos;raise&apos;,&apos;warn&apos;,None]</span></code> warning是默认值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [326]: </span><span class="n">dfb</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                            <span class="s1">&apos;three&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;six&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="s1">&apos;c&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">7</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="c"># This will show the SettingWithCopyWarning</span>
<span class="c"># but the frame values will be set</span>
<span class="gp">In [327]: </span><span class="n">dfb</span><span class="p">[</span><span class="s1">&apos;c&apos;</span><span class="p">][</span><span class="n">dfb</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&apos;o&apos;</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">42</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-485">然而，这是操作一个副本，将不工作。</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pd</span><span class="o">.</span><span class="n">set_option</span><span class="p">(</span><span class="s1">&apos;mode.chained_assignment&apos;</span><span class="p">,</span><span class="s1">&apos;warn&apos;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dfb</span><span class="p">[</span><span class="n">dfb</span><span class="o">.</span><span class="n">a</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&apos;o&apos;</span><span class="p">)][</span><span class="s1">&apos;c&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">42</span>
<span class="go">Traceback (most recent call last)</span>
<span class="go">     ...</span>
<span class="go">SettingWithCopyWarning:</span>
<span class="go">     A value is trying to be set on a copy of a slice from a DataFrame.</span>
<span class="go">     Try using .loc[row_index,col_indexer] = value instead</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-486">链接分配也可以在混合dtype帧中的设置中向上裁剪。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-487">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-488">这些设置规则适用于所有<code class="docutils literal"><span class="pre">.loc/.iloc/.ix</span></code></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-489">这是正确的访问方法</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [328]: </span><span class="n">dfc</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;A&apos;</span><span class="p">:[</span><span class="s1">&apos;aaa&apos;</span><span class="p">,</span><span class="s1">&apos;bbb&apos;</span><span class="p">,</span><span class="s1">&apos;ccc&apos;</span><span class="p">],</span><span class="s1">&apos;B&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]})</span>

<span class="gp">In [329]: </span><span class="n">dfc</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">11</span>

<span class="gp">In [330]: </span><span class="n">dfc</span>
<span class="gr">Out[330]: </span>
<span class="go">     A  B</span>
<span class="go">0   11  1</span>
<span class="go">1  bbb  2</span>
<span class="go">2  ccc  3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-490">这<em>可以</em>有时工作，但不能保证，因此应该避免</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [331]: </span><span class="n">dfc</span> <span class="o">=</span> <span class="n">dfc</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [332]: </span><span class="n">dfc</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">111</span>

<span class="gp">In [333]: </span><span class="n">dfc</span>
<span class="gr">Out[333]: </span>
<span class="go">     A  B</span>
<span class="go">0  111  1</span>
<span class="go">1  bbb  2</span>
<span class="go">2  ccc  3</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-491">这将<strong>不是</strong>工作，所以应该避免</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pd</span><span class="o">.</span><span class="n">set_option</span><span class="p">(</span><span class="s1">&apos;mode.chained_assignment&apos;</span><span class="p">,</span><span class="s1">&apos;raise&apos;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dfc</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1111</span>
<span class="go">Traceback (most recent call last)</span>
<span class="go">     ...</span>
<span class="go">SettingWithCopyException:</span>
<span class="go">     A value is trying to be set on a copy of a slice from a DataFrame.</span>
<span class="go">     Try using .loc[row_index,col_indexer] = value instead</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-492">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-493">链接的分配警告/异常旨在通知用户可能无效的分配。</span><span class="yiyi-st" id="yiyi-494">可能有假阳性；无意中报告了链式分配的情况。</span></p>
</div>
</div>
</div>
